2011-03-01 97 views
1

我有类似下面的方法:如何在UIView动画之后正确释放视图? :)

- (void)add:(id)sender { 

MyAddViewController *controller = nil; 

controller = [[MyAddViewController alloc] initWithAddType:1]; 

//controller.delegate = self; 

// Do some animation. Slide it in from the right side of the screen. 

    CGPoint initialCenter = self.view.center; 
    controller.view.center = 
     CGPointMake(initialCenter.x + 320, initialCenter.y); 

    // Add the subview now with it's center + 320 off the screen. 
    [self.view addSubview:controller.view]; 

    [UIView beginAnimations:@"animation" context:NULL]; 
    [UIView setAnimationDuration:0.35]; 
    [UIView setAnimationDelegate:self]; 
    controller.view.center = CGPointMake(initialCenter.x, initialCenter.y); 
    //[UIView setAnimationDidStopSelector:@selector(aMethodToBeCalledAfterAnimationEnd:finished:context:)]; 
    [UIView commitAnimations]; 

    //[controller release]; 
    } 

你看,我有一个控制器发布在我的add方法的最后一行。如果我取消注释此行,动画将完全死亡,并且只是将视图转储到没有动画的位置。

有没有更好的方式来做到发布,而无需创建另一个经由[UIView的setAnimationDidStopSelect做清理方法:@selector(aMethodToBeCalledAfterAnmiationEnd ...

或者我可以这样做setAnimationDidStopSelector? :@selector([controller release])?:)

在此先感谢!

回答

1

使用setAnimationDidStopSelector:是解决动画完成后释放资源的一般问题的正确方法。

虽然退后一步,重新考虑你在做什么。你为什么想要释放刚创建的控制器?如果您只是为了达到目的而创建控制器,那不是正确的做法。构建一个工厂方法来创建视图,或使用NSBundle中的方法从NIB加载视图。

0

你可以这样做:

[UIView setAnimationDelegate: controller]; 
[UIView setAnimationDidStopSelector:@selector(release)]; 
0

我看你这标记于iPhone的SDK-4.0。您可以使用新的基于块的动画来执行此操作以及其他任何清理操作。详情请参阅the documentation

+0

我试过使用新的基于块的动画,我似乎无法达到所需的效果。基本上我有_currentView_和_incomingView_应该在currentView的顶部滑动。 _incomingView_是一个全视图(320x480或其他),除了它的上半部分是透明的,所以你应该看到旧视图的底部,当它被添加为子视图。它可以与上面的动画代码一起工作,但是我不能用基于块的动画来实现它。基于块的转换似乎也会在转换过程中增加一些愚蠢的淡化效果,而上面没有。 – 2011-03-01 02:48:20