再做一些shaking动画时,通常会指定对称的左右几个点,加入都CAKeyframeAnimation.values中。
但是这就需要计算present layer的点,不太方便。
看了下CAPropertyAnimation有个属性是additive,这个属性根据当前的渲染层的值加上value生成新的渲染的值,也就是动画时present layer的值。
Determines if the value specified by the animation is added to the current render tree value to produce the new render tree value. |
Availability |
---|
下边的代码的动画效果是一样的,对比下values会有个明确的认识:
- (void)change
{
CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
animation.values = @[@0,@100,@(-100),@100,@0];
animation.keyTimes = @[@0,@(1/6.),@(3/6.),@(5/6.),@1];
animation.duration = 2.f;
animation.additive = YES; //YES把更改的值追加到当前的present层中 keypath+=value ,NO是把更改的值设置成当前present层的值keypath = value
[self.ly.layer addAnimation:animation forKey:@"shaking"];
CAKeyframeAnimation* animation1 = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
animation1.values = @[@(self.ly1.center.x),@(self.ly1.center.x + 100),@(self.ly1.center.x - 100),@(self.ly1.center.x + 100),@(self.ly1.center.x)];
animation1.keyTimes = @[@0,@(1/6.),@(3/6.),@(5/6.),@1];
animation1.duration = 2.f;
animation1.additive = NO; //YES把更改的值追加到当前的model层中 keypath+=value ,NO是把更改的值设置成当前present层的值keypath = value
[self.ly1.layer addAnimation:animation1 forKey:@"shaking"];
[button animateWithType:arc4random()%3];
[self performSelector:@selector(change) withObject:nil afterDelay:3.f];
}
原文地址:http://blog.csdn.net/growinggiant/article/details/45535587