ios - UIView 动画取消

标签 ios animation uiview touch

我想让 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 秒延迟到期之前抬起手指并再次向下触摸,消失的 View 仍然会返回,即使向下触摸也是如此。如果您随意上下触摸,动画会非常不一致。

我希望 View 仅在 1 秒后返回并且根本没有任何触摸。

最佳答案

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

关于ios - UIView 动画取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25584957/

相关文章:

iOS UIView 3D 旋转和 subview

javascript - 在 React Native 中将应用程序导航结构从版本 4 更改为版本 5

ios - 如何更改 UISearchController 中 UISearchBar 的颜色

android - Android上 View 背景颜色的动画变化

ios - 暂停和恢复 AnimateWithDuration 动画 ios

android - 机器人 :ScrollView Jerky and animation list not work

ios - AppStore 构建在 iPhone 3g 和 iPod Touch 上启动时崩溃

ios - 只有单个 uitableview 单元格可以拖动和重新排序

ios - 如何删除 UITableView 上方的空白区域?

ios - UISegmentedControl subview 未出现