【问题标题】:How to pause a UIImageView animation如何暂停 UIImageView 动画
【发布时间】:2008-10-31 11:49:19
【问题描述】:

我有一个正在使用 UIImageView 显示的动画:

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

我知道我可以使用 stopAnimating 来停止它,但我想要的是能够暂停它。原因是当您调用停止时,您的动画图像都不会显示,而我希望在我按下按钮时显示的最后一个动画图像保持不变。

我尝试将持续时间设置为更大的数字,但这也会导致所有图像消失。必须有一个非常基本的方法来做到这一点?

【问题讨论】:

    标签: iphone cocoa-touch animation uiimageview


    【解决方案1】:

    此代码将在动画过程中将动画对象暂停在其当前位置。如果您记录其他变量,例如时间或进度或您需要的任何内容,再次恢复动画应该是相当简单的。

    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.
    

    【讨论】:

    • 我也想暂停我的动画..在这段代码中 wat 是presentationLayer?
    • 这是正确的答案(应该被接受)非常感谢!
    • This 帮助我实现这一目标的答案。
    【解决方案2】:

    嗯...因为似乎没有人知道我猜这是不可能的。

    我继续写了自己的UIView,带有UIImageViewsubview,它使用NSTimer 在图像之间切换。这样做的好处是我可以在闲暇时暂停和恢复计时器,而且性能似乎不是问题。

    【讨论】:

      【解决方案3】:

      @oddmeter 只是一点点编辑:

          animatedView.animationImages = images; //images is your array
          [animatedView startAnimating];
      
      
          //Then when you need to pause;
      
      [animatedView stopAnimating]; //Important!!
          animatedView.image = [images objectAtIndex: 0];
      

      【讨论】:

      • 这仅在您碰巧在位于索引 0 的帧处暂停时才有效。
      【解决方案4】:

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

      它基本上告诉您要暂停/恢复任何基于 CALayer 的动画需要做什么。

      如果您对在UIImageView 受控动画上使用CALayer 方法感到不舒服,您总是可以自己制作基于数组的UIImage 动画。需要的代码很短,可以从这里获取:http://rssv2.blogspot.com/2011/04/animating-series-of-images-with-calayer.html

      【讨论】:

        【解决方案5】:

        另一个选项是设置图像属性以及animationImages 属性。这样做会在UIImageView 的动画停止时显示静态图像。

        假设您的类是 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];
        

        【讨论】:

          【解决方案6】:

          也许您可以截取最后一张动画图像并显示它?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-28
            • 2013-11-25
            • 2012-08-21
            相关资源
            最近更新 更多