- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title=@"test";
CALayer *layer = [[CALayer alloc] init];
layer.frame = CGRectMake(100,100,100,100);
UIImage *iconImage =[UIImage imageNamed:@"Icon57"];
layer.contents = (id)iconImage.CGImage;
layer.contentsScale = iconImage.scale;
[layer addAnimation:[ViewController moveY:30 Y:[NSNumber numberWithFloat:500.0f]] forKey:@"slideAnimation"];
[self.view.layer addSublayer:layer];
}
+(CABasicAnimation *)moveY:(float)time Y:(NSNumber *)y
{
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
animation.toValue=y;
animation.duration=time;
animation.removedOnCompletion=NO;
animation.fillMode=kCAFillModeForwards;
return animation;
}
上面代码的效果是图片在30秒,y坐标移动到了500.本人的问题是,能不能在30秒内,通过一个按钮结束动画?或有没其他的方法来提前结束动画或修改动画的时间,求帮助
解决方案:40分
动画是由时间控制的,你只要控制动画的速度和时间就可以任意控制动画的状态,要暂停动画的话只要获取当前动画执行的便宜时间就行了
CFTimeInterval currTimeoffset = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = currTimeoffset;
CFTimeInterval currTimeoffset = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = currTimeoffset;