主旨动画——弹簧动画2【起名】

核心动画——弹簧动画一重中之重介绍弹簧动画的有个别属性,上边介绍一下弹簧动画的另1种意义。

弹簧动画

率先在Main.storyboard文件之中创设三个UIButton,ViewController继承于ViewController。将UIButton设置为二个属性,选中UIButton右击不松开拖到ViewController.m文件的@interface
里面并给它起名。同时,给它贰个接触事件,将它拖到@implementation里面。具体操作看代码:

弹簧动画:CASpringAnimation->CABasicAnimation(基础动画)->CAPropertyAnimation(属性动画)->CAAnimation(核心动画)

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *annimationButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (IBAction)action:(id)sender {

    UIButton *button = sender;
    button.selected = !button.selected;
    button.backgroundColor = button.selected != YES?[UIColor colorWithRed:1.000 green:0.435 blue:0.812 alpha:1.000]:[UIColor colorWithRed:0.400 green:0.800 blue:1.000 alpha:1.000];
    [self jump];
}

- (void)jump{
    CASpringAnimation *animation = [CASpringAnimation animationWithKeyPath:@"bounds"];
    animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, self.annimationButton.frame.size.width*1.5, self.annimationButton.frame.size.height*1.5)];
    animation.mass = 2;
    animation.stiffness = 100;
    animation.damping = 3;
    animation.initialVelocity = 30;
    animation.duration = animation.settlingDuration;
    [self.annimationButton.layer addAnimation:animation forKey:@"jump"];

}

- (void)move:(CGPoint)toPoint{

    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    basicAnimation.toValue = [NSValue valueWithCGPoint:toPoint];
    basicAnimation.duration = 3;
    basicAnimation.removedOnCompletion = NO;
    basicAnimation.fillMode = kCAFillModeBoth;
    [self.annimationButton.layer addAnimation:basicAnimation forKey:@"move"];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self move:[[touches anyObject] locationInView:self.view]];
    NSLog(@"button改变位置之前的中心点x:%f y:%f",self.annimationButton.center.x,self.annimationButton.center.y);
    NSLog(@"button上面Layer的中心点x:%f y:%f",self.annimationButton.layer.position.x,self.annimationButton.layer.position.y);
    //CAAnimation 只是改变图层的动画效果,并没有真实的改变视图、图层的属性值

}
@end

下边正是基本动画的构造图:

它的机能:

起名 1

起名 2

基本功动画:CABasicAnimation->只好设置fromValue、toValue、byValue
所以CASpringAnimation(弹簧动画)也不得不设置fromValue、toValue、byValue

 

属性介绍:

    1mass 质量->影响图层运动时的弹簧惯性 质量越大 幅度越大

起名,    二stiffness 刚度(劲度/弹性) 刚度越大 形变发生的力就越大 运动越快

    叁damping 阻力 阻力越大 甘休越快

    肆initialVelocity
开端速率,动画视图的发端速度大小 速率为正数时
速度方向与移动方向1致 速率为负数时 速度方向与运动方向相反

    伍settlingDuration
得到动画完毕的预估时间

 以上正是对弹簧动画的介绍,具体操作就看上边包车型客车代码:

第2在Main.storyboard文件之中创造三个UIImageView,设置image、Mode属性,ViewController继承于ViewController。将UIImageView设置为一特性质,选中UIImageView右击不放手扶拖拉机到ViewController.m文件的@interface
里面并给它起名。

在ViewController.m文件

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *anmitionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

}

- (void)movie:(CGPoint)position{
    /*
     CASpringAnimation 弹簧动画
     animationWithKeyPath: 初始化
     addAnimation: forKey:添加到视图上的方法
     */
    CASpringAnimation *anmiation = [CASpringAnimation animationWithKeyPath:@"position"];
    //CGPoint->id CGpoint->NAValue
    anmiation.fromValue = [NSValue valueWithCGPoint:self.anmitionView.center];
    anmiation.toValue = [NSValue valueWithCGPoint:position];
    //设置fillMode 必须设置removedOnCompletion为NO
    anmiation.removedOnCompletion = NO;
    anmiation.fillMode = kCAFillModeBoth;
    //设置质量
    anmiation.mass = 2;
    //设置刚度 刚度越大 运动速率越快
    anmiation.stiffness = 50;
    //设置阻力 摩擦率
    anmiation.damping = 5;
    //设置初始速率
    anmiation.initialVelocity = 5;

    //把获得到弹簧动画的预估时间 赋给动画的持续时间
    anmiation.duration = anmiation.settlingDuration;

    [self.anmitionView.layer addAnimation:anmiation forKey:@"move"];

}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self movie:[[touches anyObject] locationInView:self.view]];
}
@end

效用如下:

起名 3

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图