简体   繁体   English

动画后如何从超级视图中删除图像

[英]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]; 让我感到困惑的是,如果我写了[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" 原因是:“ [GamePage2 removeFromSuperview]:无法识别的选择器已发送到实例”

Because you call the selector on self, instead of tempImageView1. 因为您在self而不是tempImageView1上调用选择器。

Have your tried [tempImageView1 performSelector ...] ? 您是否尝试过[tempImageView1 performSelector ...]

just for it first create one method with your above code and then call this method with delaytime 为此,请首先使用上面的代码创建一个方法,然后使用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. 中间没有任何动画循环,代码的执行不会停止,因此,当您调用“ removeFromSuperView”时,该方法甚至在动画完成之前就已执行。 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. 您可以在delay方法后使用Perform选择器,但是您必须在相应的对象(在这种情况下为GamePage2)上调用它。 Replace self with GamePage2 and it should work fine. 用GamePage2替换self,它应该可以正常工作。 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. 动画完成后,这将调用给定的选择器,您可以在其中编写从superview方法中删除的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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