CAAnimation动画实际是加在layer上,在执行完毕之后,动画效果就会消失,若要得到动画后的效果,需要加上两个属性
CGRect frame = view.frame;
view.transform = CGAffineTransformMakeTranslation(0.0, -CGRectGetMaxY(frame));
CABasicAnimation *trans = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
trans.byValue = [NSNumber numberWithFloat:CGRectGetMaxY(frame)];
trans.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale.xy"];
scale.fromValue = @0.0;
scale.toValue = @1.0;
scale.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[trans,scale];
group.duration = 0.3;
group.beginTime = CACurrentMediaTime() + animationOffset;
//动画执行后的状态模式
group.fillMode = kCAFillModeForwards;
//动画执行后不删除该状态
group.removedOnCompletion = NO;
group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
//设置动画的代理需要在加到layer之前
if (completion) {
//动画的delegate为强引用
group.delegate = self;//此delegate为strong
_CurrentComplete = completion;
}
[view.layer addAnimation:group forKey:kTranslateAndscale];
动画的代理为强引用,所以可能会引起retain circle
如果动画
group.removedOnCompletion = NO;
为yes时,此时不存在问题,但是当removedOnCompletion = NO;时,此时应该在视图移除之前移除其动画。
CASpringAnimation的坑
在使用其他动画时
group.fillMode = kCAFillModeForwards;
//动画执行后不删除该状态
group.removedOnCompletion = NO;
动画执行完毕都会得到我们想要的结果,但是设置完CASpringAnimation的这两个属性后,动画执行完毕,layer属性的变化往往的不到我们想要的结果
/* The mass of the object attached to the end of the spring. Must be greater
than 0. Defaults to one. */
@property CGFloat mass;// 质量
/* The spring stiffness coefficient. Must be greater than 0.
* Defaults to 100. */
@property CGFloat stiffness;//刚度,刚度是指材料或结构在受力时抵抗弹性变形的能力
/* The damping coefficient. Must be greater than or equal to 0.
* Defaults to 10. */
@property CGFloat damping;//阻尼
/* The initial velocity of the object attached to the spring. Defaults
* to zero, which represents an unmoving object. Negative values
* represent the object moving away from the spring attachment point,
* positive values represent the object moving towards the spring
* attachment point. */
这三个属性会影响到属性的变化值。