在 iOS5.0 中使用块,假设我们有一个 UIViewAnimationOptionAutoreverse 动画,并且需要在反向动画开始之前放置一个延迟.. 我们该怎么做呢?
感谢您对此的帮助
The best way to pause and resume animation :
add QuartzCore framework
then use the following apple's code
-(void) pause
{
CFTimeInterval pausedTime = [m_pCustomImageBtnObj.layer convertTime:CACurrentMediaTime() fromLayer:nil];
obj.layer.speed = 0.0;
obj.layer.timeOffset = pausedTime;
}
-(void) resume
{
CFTimeInterval pausedTime = [obj.layer timeOffset];
obj.layer.speed = 1.0;
obj.layer.timeOffset = 0.0;
obj.layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [obj.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
obj.layer.beginTime = timeSincePause;
}
不确定这是否正是您想要做的,但这是我用来将视图移动到屏幕上,暂停 1 秒钟,然后将视图移出屏幕的方法。
[UIView animateWithDuration:0.5 animations:^{
self.view.frame = viewFrameOnScreen;
self.view.alpha = 1.0;
} completion:^(BOOL finished){
[UIView animateWithDuration:0.5 delay:1 options:0 animations:^{
self.view.frame = viewFrameOffScreen;
self.view.alpha = 0.0f;
} completion:^(BOOL finished){}];
}];
您可以尝试这样的动画完成块:
[UIView animateWithDuration:0.6 animations:^(void)
{
self.label.bounds = CGRectMake(10, 10, 200, 200);
}completion:^(BOOL finished) {
[UIView animateWithDuration:0.6 animations:^(void)
{
self.label.bounds = CGRectMake(10, 10, 100, 100);
}completion:^(BOOL finished) {
}];
}];
您应该为此使用关键帧动画。下面是一段代码来做反转旋转:
self.animations = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"], [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"] , nil ] forKeys: [NSArray arrayWithObjects: THERE_ANIM_KEY, BACK_AGAIN_ANIM_KEY , nil ]]; CAKeyframeAnimation *kfAnim = [animations objectForKey: THERE_ANIM_KEY]; kfAnim.duration = 2; kfAnim.repeatCount = 1; kfAnim.values = [NSArray arrayWithObjects: [NSNumber numberWithFloat: 0.0 * M_PI], [NSNumber numberWithFloat: 1.0 * M_PI], [NSNumber numberWithFloat: 2.0 * M_PI], [NSNumber numberWithFloat: 2.0 * M_PI], //here is delay [NSNumber numberWithFloat: 1.0 * M_PI], [NSNumber numberWithFloat: 0.0 * M_PI], nil]; kfAnim.keyTimes = [NSArray arrayWithObjects: [NSNumber numberWithFloat: 0.0 ], [NSNumber numberWithFloat: 0.3 ], [NSNumber numberWithFloat: 0.5 ], //time of delaying in proportion of general animation duration [NSNumber numberWithFloat: 0.5 ], [NSNumber numberWithFloat: 0.8 ], [NSNumber numberWithFloat: 1.0 ], nil];
您也可以使用记住初始状态并重新设置动画
- (void)animationDidStop:(CAAnimation *)动画完成:(BOOL)标志
使用 performSelector:withObject:afterDelay: 的动画委托方法。这更简单且易于设置延迟。
RIAZ 在 Swift 3 中的回答:
func pauseAnimation() {
let pausedTime = obj.layer.convertTime(CACurrentMediaTime(), from: nil)
obj.layer.speed = 0.0
obj.layer.timeOffset = pausedTime
}
func resumeAnimation() {
let pausedTime = obj.layer.timeOffset
obj.layer.speed = 1.0
obj.layer.timeOffset = 0.0
obj.layer.beginTime = 0.0
let timeSincePause = obj.layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
obj.layer.beginTime = timeSincePause
}