【问题标题】:How to properly release a view after a UIView Animation ? :)如何在 UIView 动画之后正确释放视图? :)
【发布时间】:2011-03-01 01:35:49
【问题描述】:

我有如下方法:

- (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];
    }

你看我有一个 controller release 作为我添加方法的最后一行。如果我取消注释此行,动画将完全消失,它只是将视图转储到没有动画的位置。

有没有更好的方法来做 release 而不必创建另一个通过 [UIView setAnimationDidStopSelect:@selector(aMethodToBeCalledAfterAnmiationEnd... 进行清理的方法?

或者我可以做类似 setAnimationDidStopSelector:@selector([controller release]) 的事情吗? :)

提前致谢!

【问题讨论】:

    标签: iphone cocoa-touch memory-management ios4


    【解决方案1】:

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

    不过,请退后一步,重新考虑一下您在这里所做的事情。你为什么要释放你刚刚创建的控制器?如果您只是为了获取视图而创建控制器,那不是正确的方法。自己构建一个工厂方法来创建视图,或者使用NSBundle 中的方法从NIB 加载视图。

    【讨论】:

      【解决方案2】:

      你可以这样做:

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

      【讨论】:

        【解决方案3】:

        我看到你标记了这个 iphone-sdk-4.0。您可以使用新的基于块的动画来执行此操作和任何其他清理。详情请见the documentation

        【讨论】:

        • 我尝试过使用新的基于块的动画,但似乎根本无法达到预期的效果。基本上我有我的 currentView 并且 incomingView 应该滑入 currentView 的顶部。 incomingView 是一个完整的视图(320x480 或其他),除了它的上半部分是透明的,因此当它被添加为子视图时,您应该看到旧视图的底部。它适用于上面的动画代码,但我无法使用基于块的动画。在过渡期间,基于块的似乎也添加了一些愚蠢的淡入淡出效果,而上面没有。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-30
        • 1970-01-01
        • 2011-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多