2013-03-28 30 views
2

我正在使用[UIView animateWithDuration ...]为了显示我的应用程序的每个页面的文本。每个页面都有自己的文字。我在页面间滑动浏览。我在显示页面后使用1秒溶解效果使文本消失。如何在完成之前中断UIView动画?

下面是问题:如果我在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]; 
         }]; 

    } 

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

+0

可能重复[取消UIView动画?](http://stackoverflow.com/questions/554997/cancel-a-uiview-animation) – matt

+0

你尝试发送'removeAllAnimations'到'CurrentContent.layer'和' theReplacementContent.layer'? –

+0

@matt,虽然 – Armand

回答

2

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

[[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 

[[UIApplication sharedApplication] endIgnoringInteractionEvents]; 
0

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

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:方法。

+0

仍然有点不同。它并没有完全解决这个问题,但我怀疑我可能会做出一些错误的修改你的代码。 – Armand

+0

非常感谢你为这些行:-) – Armand