我想让 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/