看过我前两篇博客的都知道此次要将的弹簧动画(CASpringAnimation)是基于基础动画CABaseAnimation
CASpringAnimation:弹簧动画->属于基础动画CABaseAnimation的子类
CABaseAnimation只能设置fromValue和toValue/byValue
下面讲一下CASpringAnimation弹簧动画常用的属性
mass:质量 ->影响图层运动的弹簧惯性,质量越大,幅度越大
stiffness刚度(经度/弹性)刚度越大,形变产生的力就越大,运动越快
damping:阻力,阻力越大,停止越快
initialVelocity:初始速率,动画视图的初始速度大小速率为正数时,速度方向与运动方向相同,初始速度大小速率为负数时,速度方向与运动方向相反
settlingDuration 获得动画完成的预估时间
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
}
//创建弹簧动画的方法
- (void)move:(CGPoint)position{
/*
CASpringAnimation:父类CABaseAnimation->父类CAPropertyAnimation
初始化:+ (instancetype)animationWithKeyPath:(nullable NSString *)path
*/
CASpringAnimation *springAn = [CASpringAnimationanimationWithKeyPath:@"position"];
springAn.fromValue = [NSValuevalueWithCGPoint:self.imageView.center];
springAn.toValue = [NSValuevalueWithCGPoint:position];
// 设置fillModel必须把removedOnCompletion设为NO
springAn.removedOnCompletion =NO;
springAn.fillMode =kCAFillModeBoth;
/*
mass 质量
stiffness 刚度
damping 阻力
initialVelocity 初始速率
settlingDuration 获得动画完成的预估时间
*/
// 质量
springAn.mass =100;
// 刚度,刚度越大,运动速度越快
springAn.stiffness =5;
// 阻力,摩擦力
springAn.damping =2;
// 初始速率
springAn.initialVelocity =0;
//将获得弹簧的预估时间赋给持续时间
springAn.duration = springAn.settlingDuration;
// 将动画添加到图层
[self.imageView.layeraddAnimation:springAnforKey:@"move"];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[selfmove:[[touchesanyObject]locationInView:self.view]];
}
#import "ViewController.h"
@interfaceViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)button:(id)sender {
UIButton *button = sender;
button.selected = !button.selected;
button.backgroundColor = button.selected !=YES ? [UIColorredColor]:[UIColorcyanColor];
[selfjamp];
}
- (void)jamp{
CASpringAnimation *animation = [CASpringAnimationanimationWithKeyPath:@"bounds"];
animation.toValue = [NSValuevalueWithCGRect:CGRectMake(0,0,CGRectGetWidth(self.myButton.frame)*1.5,CGRectGetHeight(self.myButton.frame)*1.5)];
animation.mass =2;
animation.damping =3;
animation.initialVelocity =30;
animation.stiffness =100;
animation.duration = animation.settlingDuration;
[self.myButton.layeraddAnimation:animationforKey:@"jamp"];
}
- (void)move:(CGPoint)toPoint{
CABasicAnimation *animation = [CABasicAnimationanimationWithKeyPath:@"position"];
animation.toValue = [NSValuevalueWithCGPoint:toPoint];
animation.duration =1;
animation.removedOnCompletion =NSNotFound;
animation.fillMode =kCAFillModeBoth;
[self.myButton.layeraddAnimation:animationforKey:@""];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"buttong改变位置之前的中心点%f %f",self.myButton.center.x,self.myButton.center.y);
NSLog(@"buttong上面layer的中心点%f %f",self.myButton.layer.position.x,self.myButton.layer.position.y);
[selfmove:[[touchesanyObject]locationInView:self.view]];
NSLog(@"buttong改变位置之后的中心点%f %f",self.myButton.center.x,self.myButton.center.y);
/*
CAAnimation只是改变图层的动画效果,并没有真是我的改变图层视图的属性值
*/
}
@end