开发者

How to properly release a view after a UIView Animation ? :)

开发者 https://www.devze.com 2023-02-14 04:42 出处:网络
I have a method like the following: - (void)add:(id)sender { MyAddViewController *controller = nil; controller = [[MyAddViewController alloc] initWithAddType:1];

I have a method like the following:

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

You see I have a controller release as the last line in my add method. If I uncomment this line the animation completely dies and it just dumps the view into place with no animation.

Is there a better way to do release without开发者_Go百科 having to create another method which does cleanup via [UIView setAnimationDidStopSelect:@selector(aMethodToBeCalledAfterAnmiationEnd... ?

Or can I do something like setAnimationDidStopSelector:@selector([controller release]) ? :)

Thanks in advance!


Using setAnimationDidStopSelector: is the proper way to solve the generic problem of releasing resources after an animation completes.

Take a step back and reconsider what you're doing here, though. Why are you looking to free the controller you just created? If you are only creating the controller for the purpose of getting at the view, that isn't the right way to do it. Build yourself a factory method to create your view, or use the methods in NSBundle to load the view from a NIB.


You can do this:

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


I see you tagged this iphone-sdk-4.0. You could use the new block-based animations to do this and any other cleanup. See the documentation for details.

0

精彩评论

暂无评论...
验证码 换一张
取 消