CAPropertyAnimation additive

来源:互联网 发布:js div显示隐藏 编辑:程序博客网 时间:2024/04/30 13:52

再做一些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 = [CAKeyframeAnimationanimationWithKeyPath:@"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+=valueNO是把更改的值设置成当前present层的值keypath = value

    [self.ly.layeraddAnimation:animation forKey:@"shaking"];

    

    

    CAKeyframeAnimation* animation1 = [CAKeyframeAnimationanimationWithKeyPath:@"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+=valueNO是把更改的值设置成当前present层的值keypath = value

    [self.ly1.layeraddAnimation:animation1 forKey:@"shaking"];

    

    

    [buttonanimateWithType:arc4random()%3];

    

    

    [selfperformSelector:@selector(change)withObject:nilafterDelay:3.f];


}

1 0