【问题标题】:How to interrupt UIView animation before completion?如何在完成前中断 UIView 动画?
【发布时间】:2013-03-28 23:14:37
【问题描述】:

我正在使用 [UIView animateWithDuration... ] 来为我的应用程序的每个页面显示文本。每个页面都有自己的文本。我正在滑动以在页面之间导航。我正在使用 1 秒溶解效果让文本在页面显示后淡入。

问题出在:如果我在那 1 秒内滑动(在此期间文本淡入),动画将在下一页出现时完成,并且 2 个文本将重叠(上一个和当前)。

我想实施的解决方案是,如果我在动画发生期间碰巧滑动,则中断动画。我就是不能让它发生。 [self.view.layer removeAllAnimations];对我不起作用。

这是我的动画代码:

   - (void) replaceContent: (UITextView *) theCurrentContent withContent: (UITextView *) theReplacementContent {

    theReplacementContent.alpha = 0.0;
    [self.view addSubview: theReplacementContent];


    theReplacementContent.alpha = 0.0;

    [UITextView animateWithDuration: 1.0
                              delay: 0.0
                            options: UIViewAnimationOptionTransitionCrossDissolve
                         animations: ^{
                             theCurrentContent.alpha = 0.0;
                             theReplacementContent.alpha = 1.0;
                         }
                         completion: ^(BOOL finished){
                             [theCurrentContent removeFromSuperview];
                             self.currentContent = theReplacementContent;
                             [self.view bringSubviewToFront:theReplacementContent];
                         }];

   }

你们知道如何进行这项工作吗?你知道解决这个问题的其他方法吗?

【问题讨论】:

  • Cancel a UIView animation? 的可能重复项
  • 您是否尝试将removeAllAnimations 发送到theCurrentContent.layer 和theReplacementContent.layer?
  • @matt,虽然有点不同

标签: ios animation uiview


【解决方案1】:

您不能直接取消通过+animateWithDuration... 创建的动画。您想要做的是 替换 正在运行的动画为即时的新动画。

您可以编写以下方法,当您要显示下一页时调用该方法:

- (void)showNextPage
{
    //skip the running animation, if the animation is already finished, it does nothing
    [UIView animateWithDuration: 0.0
                          delay: 0.0
                        options: UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionBeginFromCurrentState
                     animations: ^{
                         theCurrentContent.alpha = 1.0;
                         theReplacementContent.alpha = 0.0;
                     }
                     completion: ^(BOOL finished){
                         theReplacementContent = ... // set the view for you next page
                         [self replaceContent:theCurrentContent withContent:theReplacementContent];
                     }];
}

注意传递给options: 的额外UIViewAnimationOptionBeginFromCurrentState。它的作用是,它基本上告诉框架拦截受影响属性的任何正在运行的动画,并用这个替换它们。 通过将duration: 设置为0.0,可以立即设置新值。

在completion: 块中,您可以创建和设置新内容并调用您的replaceContent:withContent: 方法。

【讨论】:

  • 仍在努力。它还没有完全解决问题,但我怀疑我可能做错了让你的代码适应我的代码。
【解决方案2】:

因此,另一种可能的解决方案是在动画期间禁用交互。

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

【讨论】:

    【解决方案3】:

    我会声明像shouldAllowContentToBeReplaced 这样的标志。动画开始时将其设置为 false,动画完成时将其设置为 true。然后在开始动画之前说if (shouldAllowContentToBeReplaced) {。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多