UIView动画取消

问题描述:

我想让UIView在触摸屏幕上离开屏幕,并在触摸之后稍微延迟回来。到目前为止:UIView动画取消

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    if ([touch view] == myMainView) 
    { 
    [disappearingView.layer removeAllAnimations]; 
    [UIView animateWithDuration:0.5 
          delay:0 
     usingSpringWithDamping:0.7 
      initialSpringVelocity:0.2 
         options:UIViewAnimationOptionBeginFromCurrentState 
        animations:^ 
    { 
     disappearingView.transform = CGAffineTransformMakeScale(0, 0); 

    } 
        completion:^(BOOL finished) 
    { 

    }]; 

    } 
} 

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    if ([touch view] == myMainView) 
    { 
     [UIView animateWithDuration:0.5 
          delay:1 
     usingSpringWithDamping:0.7 
      initialSpringVelocity:0.2 
         options:UIViewAnimationOptionBeginFromCurrentState 
        animations:^ 
    { 
     disappearingView.transform = CGAffineTransformMakeScale(1, 1); 

    } 
        completion:^(BOOL finished) 
    { 

    }]; 

    } 
} 

上面的代码到目前为止工作正常。但是,如果用户在1秒延迟到期之前抬起手指并再次触地,即使触地下降,消失的视图仍然会恢复。如果你随机触摸,动画非常不一致。

我希望视图在1秒后回来,并且根本没有任何接触。

touchesBegan中设置一个标志并在touchesEnded中再次清除标志。在touchesEnded的动画块中,检查该标志,如果该标志已设置,则不要重新缩放至(1,1)。这样,如果在第一次结束之后但动画完成之前再次触摸它,则不会再回来。

+0

我正在尝试这个过程。 DELAY的最佳做法是什么?我应该通过延迟动画来做到吗?或者做[self performSelector:@selector(touchUpAnimations)withObject:nil afterDelay:1]; ? – Gizmodo 2014-08-30 18:48:39

+0

我不认为这有很大的区别。我的偏好是让动画延迟(正如你现在所做的那样),但以另一种方式进行则会避免动画“无所作为”,如果发生了另一次触摸。 – pbasdf 2014-08-30 19:30:44