How to pause a UIImageView animation

前端 未结 6 1735
有刺的猬
有刺的猬 2021-02-06 17:03

I have an animation that I\'m displaying using a UIImageView:

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


        
相关标签:
6条回答
  • 2021-02-06 17:30

    @oddmeter just a little edit:

        animatedView.animationImages = images; //images is your array
        [animatedView startAnimating];
    
    
        //Then when you need to pause;
    
    [animatedView stopAnimating]; //Important!!
        animatedView.image = [images objectAtIndex: 0];
    
    0 讨论(0)
  • 2021-02-06 17:30

    Another option is to set the image property as well as the animationImages property. Doing this will display the static image when the UIImageView has its animations stopped.

    Assuming your class is a subclass of UIImageView and have an NSMutableArray of images, do the following:

    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];
    
    0 讨论(0)
  • 2021-02-06 17:44

    Maybe you can take a screenshot of the last animated image and display that?

    0 讨论(0)
  • 2021-02-06 17:50

    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.
    
    0 讨论(0)
  • 2021-02-06 17:50

    This should do the trick: 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.

    If you feel uncomfortable using CALayer methods on UIImageView controlled animation, you could always just make the UIImage array based animation yourself. 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

    0 讨论(0)
  • 2021-02-06 17:53

    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. 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.

    0 讨论(0)
提交回复
热议问题