简体   繁体   English

如何暂停UIImageView动画

[英]How to pause a UIImageView animation

I have an animation that I'm displaying using a UIImageView: 我有一个动画,我正在使用UIImageView显示:

imageView.animationImages = myImages;
imageView.animationDuration = 3;
[imageView startAnimating];

I know I can stop it using stopAnimating, but what I want is to be able to pause it. 我知道我可以使用stopAnimating来阻止它,但我想要的是能够暂停它。 The reason is that when you call stop, none of your animation images are displayed, whereas I would like the last one that is up at the time when I hit a button to stay up. 原因是当你调用stop时,你的动画图像都没有显示出来,而我希望最后一个在我按下按钮时保持最新状态。

I have tried setting the duration to a much larger number, but that causes all the images to disappear as well. 我已经尝试将持续时间设置为更大的数字,但这会导致所有图像也消失。 There must be a really basic way to do this? 必须有一个非常基本的方法来做到这一点?

This code will pause an animated object at its current position in the animation process. 此代码将在动画过程中暂停动画对象的当前位置。 If you record other variables like the time or progress or whatever you need, it should be fairly trivial to resume the animation again. 如果你记录其他变量,如时间或进度或你需要的任何东西,再次恢复动画应该是相当简单的。

UIView *viewBeingAnimated = //your view that is being animated
viewBeingAnimated.frame = [[viewBeingAnimated.layer presentationLayer] frame];
[viewBeingAnimated.layer removeAllAnimations];
//when user unpauses, create new animation from current position.

Hmmm...since no one seems to know I guess it's not possible. 嗯...因为似乎没有人知道我猜这是不可能的。

I went ahead and wrote my own UIView , with a UIImageView subview , that uses an NSTimer to switch between images. 我继续编写自己的UIView ,使用UIImageView subview ,使用NSTimer在图像之间切换。 The advantage of this is that I can pause and resume the timer at my leisure, and performance doesn't seem to be an issue. 这样做的好处是我可以在闲暇时暂停和恢复计时器,性能似乎不是问题。

@oddmeter just a little edit: @oddmeter只是一点编辑:

    animatedView.animationImages = images; //images is your array
    [animatedView startAnimating];


    //Then when you need to pause;

[animatedView stopAnimating]; //Important!!
    animatedView.image = [images objectAtIndex: 0];

This should do the trick: https://developer.apple.com/library/ios/#qa/qa2009/qa1673.html 这应该可以解决问题: https//developer.apple.com/library/ios/#qa/qa2009/qa1673.html

It basically tells you what you need to do to pause/resume any CALayer based animation. 它基本上告诉您需要做什么来暂停/恢复任何基于CALayer的动画。

If you feel uncomfortable using CALayer methods on UIImageView controlled animation, you could always just make the UIImage array based animation yourself. 如果您对UIImageView控制的动画使用CALayer方法感到不舒服,您可以随时自己制作基于UIImage数组的动画。 The code needed is very short, you can take it from here: http://rssv2.blogspot.com/2011/04/animating-series-of-images-with-calayer.html 所需的代码很短,您可以从这里获取: http//rssv2.blogspot.com/2011/04/animating-series-of-images-with-calayer.html

Another option is to set the image property as well as the animationImages property. 另一个选项是设置图像属性以及animationImages属性。 Doing this will display the static image when the UIImageView has its animations stopped. UIImageView的动画停止时,执行此操作将显示静态图像。

Assuming your class is a subclass of UIImageView and have an NSMutableArray of images, do the following: 假设您的类是UIImageView的子类并且具有NSMutableArray图像,请执行以下操作:

self.animationImages = images;
//yes, I'm skipping the step to check and make sure you have at least one
//element in your array
self.image = [images objectAtIndex: 0];

也许你可以截取最后一个动画图像的截图并显示出来?

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

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