简体   繁体   中英

How to remove image from superview after animation

I am making an app in which when the user is clicking an image it is shown, and afterwards comes the next code. As you can see it fades the image away and then i want to remove it from the superview.

This is the code:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8f];
[UIView setAnimationDelay:1.0f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
tempImageView1.alpha = 0;
tempImageView2.alpha = 0;
[UIView commitAnimations];

[tempImageView1 removeFromSuperview];
[tempImageView2 removeFromSuperview];
//[self performSelector:@selector(removeFromSuperview) withObject:tempImageView1 afterDelay:1.8f];
//[self performSelector:@selector(removeFromSuperview) withObject:tempImageView2 afterDelay:1.8f];

The thing that puzzles me, is that if I write [tempImageView1 removeFromSuperview]; the app works but of course the image closes before it is even shown. When i try to write one of the commented lines above, to do the same but with a delay, i get an error msg.

The reason is: "[GamePage2 removeFromSuperview]: unrecognized selector sent to instance"

Because you call the selector on self, instead of tempImageView1.

Have your tried [tempImageView1 performSelector ...] ?

just for it first create one method with your above code and then call this method with delaytime

[self performSelector:@selector(removeImage) withObject:nil afterDelay:2.0];

-(void)removeImage{

     [tempImageView1 removeFromSuperview];
     [tempImageView2 removeFromSuperview];
     CATransition *animation = [CATransition animation];
 [animation setDelegate:self];  
 [animation setType:kCATransitionFade];
 [animation setDuration:0.5];
 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:
                              kCAMediaTimingFunctionEaseInEaseOut]];
  [[tempImageView1 layer] addAnimation:animation forKey:@"transitionViewAnimation"];
 [[tempImageView2 layer] addAnimation:animation forKey:@"transitionViewAnimation"];
}

In your code just do

tempImageView1.alpha = 1;
tempImageView2.alpha = 1;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8f];
[UIView setAnimationDelay:1.0f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
tempImageView1.alpha = 0;
tempImageView2.alpha = 0;
[tempImageView1 removeFromSuperview];
[tempImageView2 removeFromSuperview];
[UIView commitAnimations];

Execution of your code is not stopped for any animation loops that you have in the middle and hence, when you call "removeFromSuperView", that method is executed even before the animation is completed. That leads to your image getting removed without any animation. You can use the perform selector after delay method but you will have to call it on the corresponding object which is the GamePage2 in this case. Replace self with GamePage2 and it should work fine. A better way is to use animation completion handler which can be done like below

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.1.8];
    [UIView setAnimationDelegate: self];
    [UIView setAnimationDidStopSelector:@selector(animationFinished:context:)];

    //your animation code... 
    [UIView commitAnimations];

This will call the given selector after the animation is completed and you can write your remove from superview method there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM