【问题标题】:UIView Animation CancelUIView 动画取消
【发布时间】:2014-08-30 17:17:43
【问题描述】:

我想让 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 秒后返回,并且根本没有任何触摸。

【问题讨论】:

    标签: ios animation uiview touch


    【解决方案1】:

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

    【讨论】:

    • 我正在尝试这个。 DELAY 的最佳做法是什么。我应该通过延迟动画来做到这一点吗?或做 [self performSelector:@selector(touchUpAnimations) withObject:nil afterDelay:1]; ?
    • 我认为差别不大。我的偏好是让动画进行延迟(就像您目前所做的那样),但如果发生另一次触摸,则以另一种方式执行此操作将避免动画“什么都不做”。
    猜你喜欢
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多