本文介绍了如何在完成之前中断UIView动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using [UIView animateWithDuration... ] in order to display a text for each page of my app. Each page got its own text. I'm swiping to navigate between pages. I am using a 1 second dissolve effect to have the text fading in after the page is displayed.

这是问题所在:如果我在这1秒钟内(在文本淡入的过程中)刷卡,动画将在下一页出现时完成,并且2个文本将重叠(上一个和当前).

Here's the problem: If I'm swiping during that 1 second (during which the text is fading in) the animation will complete when the next page appears and 2 texts will overlap (the previous and the current).

我想实现的解决方案是,如果我在动画发生期间碰巧滑动动画,则要中断它.我只是做不到.[self.view.layer removeAllAnimations];对我不起作用.

The solution I'd like to implement is to interrupt the animation if I happen to swipe during its occurrence. I just cannot make it happen. [self.view.layer removeAllAnimations]; does not work for me.

这是我的动画代码:

   - (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];
                         }];

   }

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

Do you guys know how to make this work? Do you know any other way to address this issue?

推荐答案

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

You can not directly cancel animations created via +animateWithDuration.... What you want to do is replace the running animation with an instant new one.

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

You could write the following method, that get's called when you want to show the next page:

- (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 ,可以立即设置新值.

Notice the additional UIViewAnimationOptionBeginFromCurrentState passed to options:. What this does is, it basically tells the framework to intercept any running animations for the affected properties and replace them with this one. By setting the duration: to 0.0 the new values are set instantly.

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

In the completion: block you can then create and set your new content and call your replaceContent:withContent: method.

这篇关于如何在完成之前中断UIView动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 23:13