【问题标题】:Issue Resuming UIVIew Animation: ios7?问题恢复 UIVIew 动画:ios7?
【发布时间】:2023-04-11 11:53:01
【问题描述】:

当视图加载时,我通过调用方法 animateSettings 来为 UIImageView 设置动画,该方法执行以下操作:

- (void)animateSettings {

    NSLog(@"Animate Settings");
    [UIView animateWithDuration:1.0f
                      delay:0.0f
                    options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewKeyframeAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState)
                 animations:^{
                     self.waterImageView.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75);
                 }
                 completion:nil];
}

问题是当我退出应用程序并返回时,动画会冻结。我明白为什么会这样,但我似乎不知道如何恢复它。

我在 viewDidLoad 方法中设置了NSNotifications,以便在视图变为活动状态或进入后台时通知我。执行以下操作我调用animateSettings 方法或removeAnimation 方法。

[[NSNotificationCenter defaultCenter]addObserver:self
                                        selector:@selector(animateSettings)
                                            name:UIApplicationDidBecomeActiveNotification
                                          object:nil];

[[NSNotificationCenter defaultCenter]addObserver:self
                                        selector:@selector(removeAnimation)
                                            name:UIApplicationDidEnterBackgroundNotification
                                          object:nil];

这是我的 removeAnimation 方法

- (void)removeAnimation {
    NSLog(@"Remove Animation");
    [self.waterImageView.layer removeAllAnimations];
}

问题:即使方法被正确调用(NSLogging 有效),动画仍然被冻结。

【问题讨论】:

  • 进入后台时无需调用removeAllAnimations。它是系统为您调用的。

标签: ios objective-c animation uiview uiimageview


【解决方案1】:

你第一次开始制作动画(我们称之为1)动画代码说:

self.waterImageView.center = 
    CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75);

现在假设您进入后台并返回。再次调用您的动画代码(我们称之为 2):

self.waterImageView.center = 
    CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75);

好的,但是在 2 期间,self.waterImageView 现在在哪里?它已经 在 CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75) - 因为那是你在 1 期间放置它的地方!

因此没有任何反应,因为如果可动画属性的值没有更改,则没有动画。

知道了这一点,解决方案就很明显了。当您进入后台时,或者如果动画因任何原因停止,将视图放回原来的位置。现在,当您再次启动动画时,视图就有了动画的位置。

【讨论】:

  • 谢谢马特。但是,请记住,我会自动反转动画并不断循环播放。我也使用 UIViewAnimationOptionBeginFromCurrentState 作为选项,但它不会恢复飞行中的动画。也许我错了,但如果确实是你说的那样,它应该会自动自动反转,对吗?
  • 您在动画中指定的位置是视图真正呈现的位置;动画只是表示层正在做的事情。并且无法在飞行中恢复,因为没有可以恢复的飞行动画 - 你删除了它! (无论如何系统都会将其删除。)
  • 另见我书中对此类动画的讨论:apeth.com/iOSBook/ch17.html#SECblockbasedviewanimation
  • 感谢您的解释。我对演示文稿和位置的实际变化感到困惑。我完全按照您的建议进行了操作,然后在 removeAnimation 方法中将视图设置回其原始位置。欣赏解释!
【解决方案2】:

有两种类型的错误动画停止..

1-)当新的vc打开时,弹出它停止..

解决方案: 在 vi​​ewDidAppear 上调用动画可以解决这个问题 问题

2-) 当应用程序进入后台时......然后进入前台 动画不工作

这是我的解决方案,它适用于 ios14:

1-) 实施协议

protocol ProtocolStartAnimation {
    func startAnimationAfterBackground()
}

2-) 在 AppDelegate 中实现以下代码:

  @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {


        var window: UIWindow?
    
        var genelDelegate : ProtocolStartAnimation?
  
        func applicationWillEnterForeground(_ application: UIApplication) {
     
        genelDelegate?.startAnimationAfterBackground()
    }

}

3-) 在 ViewController 上实现委托协议

class MainRootVC: UIViewController,
                  ProtocolStartAnimation  {

    func startAnimationAfterBackground() {
        startAnimation()
    }

}



override func viewDidLoad() {
            
   let appDelegate = UIApplication.shared.delegate as! AppDelegate
   appDelegate.genelDelegate = self
            
  }

【讨论】:

    猜你喜欢
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    相关资源
    最近更新 更多