user1824518
user1824518

Reputation: 223

Not able to remove image from superview

I've created an animation that, when finished, is supposed to remove the image being animated. For some reason, it's not being working (i.e. it's not removing the image). Is there something I'm doing wrong?:

[UIView animateWithDuration:1.8 delay:0 options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     appleView[i].frame = CGRectMake(applePosition, 400.0, 25.0, 25.0);
                 }
                 completion:^(BOOL finished){ [appleView[i] removeFromSuperview]; }];

Upvotes: 0

Views: 116

Answers (3)

Stripes
Stripes

Reputation: 4229

Is appleView a local variable, or a class member? Does anything else alter appleView?

The completion block will run around 1.8 seconds after you start the animation, if appleView is a class member and something changes it you could be removing the wrong thing. Try capturing appleView[i] in a local variable, like this:

UIView *goingAway = appleView[i];
[UIView animateWithDuration:1.8 delay:0 options:UIViewAnimationOptionCurveEaseInOut
       animations:^{
           goingAway.frame = CGRectMake(applePosition, 400.0, 25.0, 25.0);
        } completion:^(BOOL finished){
           [goingAway removeFromSuperview];
        }];

Upvotes: 0

Paras Joshi
Paras Joshi

Reputation: 20541

try with this code ..

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.8];
[UIView setAnimationTransition: UIViewAnimationOptionCurveEaseInOut forView:self.view cache:YES];
[appleView[i] removeFromSuperview];
[UIView commitAnimations];

i hope this help you...

Upvotes: 0

Max
Max

Reputation: 16719

The possible reason is that appleView[i] is nil. Put a breakpoint inside completion block and check that.

Upvotes: 1

Related Questions