我有一个动画块来执行一个简单的基于变换的动画,完成后会从其超级视图中删除有问题的视图。
UIView *msgView = [[UIView alloc] initWithFrame:CGRectMake(160, 120, 160, 100)];
// Do stuff to set up the subviews of msgView.
// Add the msgView to the superview (ViewController) that is going to display it.
CATransform3D transform = CATransform3DMakeScale(2.5, 2.5, 1.0);
[UIView animateWithDuration:5.0
animations:^(void){msgView.layer.transform = transform;}
completion:^(BOOL finished){[msgView removeFromSuperview];}];
然后,我使用 Tech Q&A 1673 http://developer.apple.com/library/ios/#qa/qa1673/_index.html中详述的代码 来暂停动画。
-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}
但是,此代码不会阻止完成代码的执行。因此,为了防止代码执行,我将完成代码更改为:
completion:^(BOOL finished){if(finished == TRUE)[msgView removeFromSuperview];};
在检查完成 == TRUE 时,可防止在动画块暂停时执行完成代码。如果在“取消暂停”动画之前超过了持续时间,则不会执行完成代码。即在这种情况下,msgView 保留在超级视图中。
无论如何要暂停/取消暂停与完成代码相关的动画和计时器(如果这是发生的事情)?