2

我正在使用 [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];
                         }];

   }

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

4

3 回答 3

11

您不能直接取消通过+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];
                     }];
}

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

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

于 2013-03-29T00:12:00.787 回答
2

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

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

[[UIApplication sharedApplication] endIgnoringInteractionEvents];
于 2013-03-28T23:20:55.630 回答
0

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

于 2013-03-28T23:26:31.267 回答