今天对iOS动画再学习时,发现有个知识空白。这就是iOS 9 新出的CASpringAnimation,是苹果专门解决开发者关于弹簧动画的这个需求而封装的类。CASpringAnimation 继承于CABaseAnimation。下面我们就详细解说一下有关CASpringAnimation类的相关属性和用法。
CASpringAnimation相关属性
#pragma CASpringAnimation 弹簧动画 的相关属性
/* 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. */
//初始速率,动画视图的初始速度大小 Defaults to zero
//速率为正数时,速度方向与运动方向一致,速率为负数时,速度方向与运动方向相反
@property CGFloat initialVelocity;
/* Returns the estimated duration required for the spring system to be
* considered at rest. The duration is evaluated for the current animation
* parameters. */
//估算时间 返回弹簧动画到停止时的估算时间,根据当前的动画参数估算
@property(readonly) CFTimeInterval settlingDuration;
CASpringAnimation解析
CASpringAnimation初始化
初始化CASpringAnimation一般使用animationWithKeyPath:
方法,这里的KeyPath
可以是@"position"
这用点表示路径@"position.x"
用x轴表示路径@"position.y"
用y轴表示路径@"bounds"
这个表示会改变对象的宽高
/* Creates a new animation object with its `keyPath' property set to
* 'path'. */
CASpringAnimation *springAnimation = [CASpringAnimation animationWithKeyPath:@"bounds"];
CASpringAnimation属性赋值
mass
模拟的是质量,影响图层运动时的弹簧惯性,质量越大,弹簧拉伸和压缩的幅度越大 默认值:1 ;
springAnimation.mass = 5;
stiffness
刚度系数(劲度系数/弹性系数),刚度系数越大,形变产生的力就越大,运动越快。默认值: 100 ;
springAnimation.stiffness = 100;
damping
阻尼系数,阻止弹簧伸缩的系数,阻尼系数越大,停止越快。默认值:10;
springAnimation.damping = 10;
initialVelocity
初始速率,动画视图的初始速度大小。默认值:0 ;
速率为正数时,速度方向与运动方向一致,速率为负数时,速度方向与运动方向相反;
springAnimation.initialVelocity = 10;
settlingDuration
估算时间 返回弹簧动画到停止时的估算时间,根据当前的动画参数估算;
springAnimation.duration = springAnimation.settlingDuration;
removedOnCompletion
默认为YES 。当设置为YES时,动画结束后,移除layer层的;当设置为NO时,保持动画结束时layer的状态;
//Determines if the animation is removed from the target layer’s animations upon completion.
springAnimation.removedOnCompletion = NO;
fillMode
属于QuartzCore动画中的的属性,文章末尾我会详细谈谈。
springAnimation.fillMode = kCAFillModeBoth;
addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key
将动画添加到视图的layer层,“key”作为这个动画的唯一标示符,可以是任意唯一的字符串或为空;
[self.jellyView.layer addAnimation:springAnimation forKey:@"springAnimation"];
实现代码展示
#pragma mark iOS9 CASpringAnimation 弹簧动画
- (void)springAnimationTextAction:(CGPoint)point {
CASpringAnimation *springAnimation = [CASpringAnimation animationWithKeyPath:@"bounds"];
//路径计算模式 (@"position")
if ([springAnimation.keyPath isEqualToString:@"position"]) {
springAnimation.fromValue = [NSValue valueWithCGPoint:self.jellyView.layer.position];
springAnimation.toValue = [NSValue valueWithCGPoint:point];
}else if ([springAnimation.keyPath isEqualToString:@"position.x"]) {
springAnimation.fromValue = @(self.jellyView.layer.position.x);
springAnimation.toValue = @(point.x);
}else if ([springAnimation.keyPath isEqualToString:@"position.y"]) {
springAnimation.fromValue = @(self.jellyView.layer.position.y);
springAnimation.toValue = @(point.y);
}else if ([springAnimation.keyPath isEqualToString:@"bounds"]) {
// CGFloat width = arc4random()%1000*0.1f+20.0f;
// CGFloat height = arc4random()%30*0.1f;
springAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(point.x, point.y, 60, 60)];
springAnimation.toValue = [NSValue valueWithCGRect:self.jellyView.frame];
}
//质量,影响图层运动时的弹簧惯性,质量越大,弹簧拉伸和压缩的幅度越大 Defaults to one
springAnimation.mass = 5;
//刚度系数(劲度系数/弹性系数),刚度系数越大,形变产生的力就越大,运动越快 Defaults to 100
springAnimation.stiffness = 100;
//阻尼系数,阻止弹簧伸缩的系数,阻尼系数越大,停止越快 Defaults to 10
springAnimation.damping = 10;
//初始速率,动画视图的初始速度大小 Defaults to zero
//速率为正数时,速度方向与运动方向一致,速率为负数时,速度方向与运动方向相反
springAnimation.initialVelocity = 10;
//估算时间 返回弹簧动画到停止时的估算时间,根据当前的动画参数估算
NSLog(@"====%f",springAnimation.settlingDuration);
springAnimation.duration = springAnimation.settlingDuration;
//removedOnCompletion 默认为YES 为YES时,动画结束后,恢复到原来状态
springAnimation.removedOnCompletion = NO;
// springAnimation.fillMode = kCAFillModeBoth;
[self.jellyView.layer addAnimation:springAnimation forKey:@"springAnimation"];
}
这个动画是animationWithKeyPath:(nullable NSString *)path
path为@"bounds"时
CASpringAnimation *springAnimation = [CASpringAnimation animationWithKeyPath:@"bounds"];
这个动画是
animationWithKeyPath:(nullable NSString *)path
path为@"position"时
CASpringAnimation *springAnimation = [CASpringAnimation animationWithKeyPath:@"position"];
这个动画是
animationWithKeyPath:(nullable NSString *)path
path为@"position.y"时
CASpringAnimation *springAnimation = [CASpringAnimation animationWithKeyPath:@"position.y"];
CASpringAnimation 弹簧动画源码下载
知识点扩充
fillMode属性的设置:
kCAFillModeRemoved 这个是默认值,也就是说当动画开始前和动画结束后,
动画对layer都没有影响,动画结束后,layer会恢复到之前的状态
kCAFillModeForwards 当动画结束后,layer会一直保持着动画最后的状态
kCAFillModeBackwards 在动画开始前,只需要将动画加入了一个layer,
layer便立即进入动画的初始状态并等待动画开始。
kCAFillModeBoth 这个其实就是上面两个的合成.动画加入后开始之前,
layer便处于动画初始状态,动画结束后layer保持动画最后的状态