iOS CoreAnimation随笔

2017-05-26  本文已影响0人  师从小马哥

平移动画

// 设置的keyPath是@"position",说明要修改的是CALayer的position属性,也就是会执行平移动画
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];

animation.duration = 1.5;

animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 80)];
// toValue换成byValue,代表CALayer从位置(50, 80)开始向右移动300、向下移动350,也就是移动到位置(350, 430)
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 350)];

animation.delegate = self;
// 动画执行完毕后,动画会自动从CALayer上移除,CALayer又会回到原来的状态。为了保持动画执行后的状态
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;

// @"translate"是给动画对象起个名称,以后可以调用CALayer的removeAnimationForKey:方法根据动画名称停止相应的动画
[view.layer addAnimation:animation forKey:@"translate"];

上一篇 下一篇

猜你喜欢

热点阅读