iOS9 CASpringAnimation弹簧动画详解

iOS9 CASpringAnimation弹簧动画详解

1.CASpringAnimation

  • iOS9才引入的动画类,它继承于CABaseAnimation,用于制作弹簧动画。

2.构建过程

	#import "ViewController.h"
	
	@interface ViewController ()
	
	@property (nonatomic,strong) UIView * newView;
	
	@end
	
	@implementation ViewController
	
	- (UIView *)newView{
	    if (!_newView) {
	        _newView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
	        _newView.backgroundColor = [UIColor greenColor];
	    }
	    return _newView;
	}
	
	- (void)viewDidLoad {
	    [super viewDidLoad];
	    
	    [self.view addSubview:self.newView];
	    
	}
	
	- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
	    CASpringAnimation * animation = [CASpringAnimation animation];
	    
	    animation.keyPath = @"position.x";
	    animation.damping = 5;
	    animation.mass = 1;
	    animation.initialVelocity = 0;
	    animation.fromValue = [NSValue valueWithCGPoint:self.newView.layer.position];
	    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
	    animation.duration = animation.settlingDuration;
	    animation.removedOnCompletion = NO;//?不好使
	    [self.newView.layer addAnimation:animation forKey:animation.keyPath];
	
	}
	
	@end

3参数说明

mass:

质量,影响图层运动时的弹簧惯性,质量越大,弹簧拉伸和压缩的幅度越大,如果把质量改成10,则动画的速度变慢,并且波动幅度变大。

stiffness:

刚度系数(劲度系数/弹性系数),刚度系数越大,形变产生的力就越大,运动越快。

damping

阻尼系数,阻止弹簧伸缩的系数,阻尼系数越大,停止越快。

initialVelocity:

初始速率,动画视图的初始速度大小速率为正数时,速度方向与运动方向一致,速率为负数时,速度方向与运动方向相反,如果把速率改成-20,则View会先往左边移动一小段距离,然后再开始往右移动,因为初始速度往左,所以会经历先减速后加速再减速的过程。

settlingDuration:

结算时间,返回弹簧动画到停止时的估算时间,根据当前的动画参数估算,通常弹簧动画的时间使用结算时间比较准确。

posted on 2016-05-20 17:46  潇楠  阅读(497)  评论(0)    收藏  举报

导航