【问题标题】:UIView animation stops when view disappears, doesn't resume when it re-appearsUIView 动画在视图消失时停止,当它重新出现时不会恢复
【发布时间】:2013-09-05 03:41:01
【问题描述】:

这是我的配置:

我有一个带有 ListViewController 的故事板。 ListViewController 有一个名为 EmptyListView 的子视图 UIView。 EmptyListView只有在UITableView中没有item时才会显示,否则隐藏。

EmptyListView 有一个名为 emptyListViewArrow 的 UIImageView 子视图,它在视觉上指向按钮以在我的 UITableView 中创建一个新条目。此箭头以向上和向下的方式无限动画化。

我监听 EmptyListView 在完成子视图布局后发送通知。我这样做是因为如果不是这样,改变约束的动画就会出现不正确的行为。

- (void) layoutSubviews {
    [super layoutSubviews];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"EmptyViewDidLayoutSubviews" object:nil];
}

当 ListViewController 观察到这个通知时,它开始动画:

[UIView animateWithDuration:0.8f delay:0.0f options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse) animations:^{
    self.emptyListViewArrowVerticalSpace.constant = 15.0f;
    [emptyListView layoutIfNeeded];
} completion:nil];

如果我将应用程序置于后台或将另一个 ViewController 推入堆栈,一旦我回到应用程序或 ListViewController,动画就会停止/暂停。我无法让它重新开始。

我试过了:

  1. 监听应用程序将退出/变为活动状态。辞职时,我做了[self.view.layer removeAllAnimations],然后尝试使用上面的代码再次启动动画。
  2. 删除正在动画的 UIImageView 并将其添加回父视图,然后尝试启动动画。

我很遗憾在这里使用约束,但希望能深入了解可能存在的问题。

谢谢!

【问题讨论】:

  • 你在使用viewDidLoadviewWillAppear中的方法吗?
  • viewDidAppear,但不是出于任何特殊原因。

标签: iphone ios ios6 uiview uiviewanimation


【解决方案1】:

我遇到了同样的问题。通过以下方式解决。

yourCoreAnimation.isRemovedOnCompletion = false

或者你有一组动画

yourGroupAnimation.isRemovedOnCompletion = false

【讨论】:

  • 你知道它是否会阻止视图控制器释放吗?
  • 有效!谢谢!如果定义了 CATransaction 的完成块-它将不起作用。在我的情况下,我完成并添加了 isRemovedOnCompletion - 它为我解决了问题。
【解决方案2】:

我不确定你在用那个通知做什么。我已经做了很多有约束的动画,而且从来没有这样做过。我认为问题在于,当您离开视图时,约束的常量值为 15(我已经通过 viewWillDisappear 中的日志验证了这一点),因此将其设置为 15 的动画将无济于事。在一个测试应用程序中,我在 viewWillAppear 中将常量设置回它的起始值 (0),它工作正常:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.bottomCon.constant = 0;
    [self.view layoutIfNeeded];
}


-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [UIView animateWithDuration:1 delay:0.0f options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse  animations:^{
        self.bottomCon.constant = -40.0f;
        [self.view layoutIfNeeded];
    } completion:nil];
}

【讨论】:

    【解决方案3】:

    这是缺点。 要克服这个问题,您必须再次调用动画方法。

    你可以这样做:

    在控制器中添加观察者

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

    这将在您恢复应用后被调用。

    添加此方法:

    - (void)resumeAnimation:(NSNotification *)iRecognizer {
        // Restart Animation
    }
    

    希望这会对您有所帮助。

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题。我将动画应用到 viewDidLoad 中的 imageView。

      UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat, .autoreverse, ], animations: {
      
          self.myImageView.transform = self.transform
          self.myImageView.alpha = 0.7
      
        }
      

      但每当我推了其他 VC 并回来时,动画就不起作用

      什么对我有用

      1. 我不得不像这样重置viewWillDisappear 中的动画

        myImageView.transform = .identity

        myImageView.alpha = 1

      2. 将上述动画块 (UIView.animate()) 写入 viewWillAppear ALSO

      希望对你有帮助

      【讨论】:

        【解决方案5】:

        当应用程序进入后台并再次返回前台时,您将面临同样的问题:

        斯威夫特:

        NotificationCenter.default.addObserver(self, selector: #selector(enterInForeground), name: NSNotification.Name(rawValue: NSNotification.Name.UIApplicationWillEnterForeground.rawValue), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(enterInBackground), name: NSNotification.Name(rawValue: NSNotification.Name.UIApplicationDidEnterBackground.rawValue), object: nil)
        
        func enterInForeground() {
             self.animateTheLaserLine()
        }
        func enterInBackground() {
             // reset the position when app enters in background
             // I have added the animation on layout constrain you can take your 
             // component 
             self.laserTopConstrain.constant = 8
        }
        

        【讨论】:

          猜你喜欢
          • 2019-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多