ios calayer的动画 怎么延时
2016-06-18 · 百度知道合伙人官方认证企业
ios calayer的动画延时是通过UIView animateWithDuration设置delay参数实现的。
具体代码如下:
[UIView animateWithDuration:element.duration
delay:element.delay
options:UIViewAnimationOptionCurveLinear
animations:^{
//设置动画属性
}
} completion:^(BOOL finished){
// 重置开始时间
//当暂停启动后,重新设置启动时间。
**self.layer.beginTime = 0.0f;**
}];
在ios4.0及以后鼓励使用animateWithDuration方法来实现动画效果。
函数原型:
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
参数说明:
duration为动画持续的时间。
animations为动画效果的代码块。
下面是可以设置动画效果的属性:
frame
bounds
center
transform
alpha
backgroundColor
contentStretch
例如一个视图淡出屏幕,另外一个视图出现的代码:
[UIView animateWithDuration:1.0 animations:^{ firstView.alpha = 0.0; secondView.alpha = 1.0; }];
completion为动画执行完毕以后执行的代码块
options为动画执行的选项。可以参考这里
delay为动画开始执行前等待的时间
-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
//恢复layer上的动画
-(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;
}