CABasicAnimation-04-核心动画-day4

//
//  ViewController.m
//  04-CABasicAnimation
//
//  Created by mac on 16/4/18.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self createBasicAnimation];
}
- (void)createBasicAnimation {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    view.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:view];
    
    //1. 初始化动画对象 : keyPath是在动画过程中,需要改变的值
    CABasicAnimation *basicAnim = [CABasicAnimation animationWithKeyPath:@"position"];
    
    //2. 设置属性改变的值
    basicAnim.fromValue = [NSValue valueWithCGPoint:CGPointZero];
    basicAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
    
    //3. 持续时间
    basicAnim.duration = 1;
    
    //4. 将动画添加到图层上
    [view.layer addAnimation:basicAnim forKey:@"basic"];
}

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

//    [self transitionAnimation];
    [self scaleAnimation];
}

- (void)transitionAnimation {
    
    //1. 初始化动画对象 : keyPath是在动画过程中,需要改变的值
    CABasicAnimation *basicAnim = [CABasicAnimation animationWithKeyPath:@"position"];
    
    //2. 设置属性改变的值
    basicAnim.fromValue = [NSValue valueWithCGPoint:CGPointZero];
    basicAnim.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
    basicAnim.byValue = [NSValue valueWithCGPoint:CGPointMake(100, 400)];
    
    //3. 持续时间
    basicAnim.duration = 1;
    //4. 将动画添加到图层上
    [self.imageView.layer addAnimation:basicAnim forKey:@"basic"];
}

- (void)scaleAnimation {
    
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    basic.fromValue = @1;
    basic.toValue = @5;
    
    basic.duration = 1.0f;
    
    //开启动画回放
    basic.autoreverses = YES;
    
    [self.imageView.layer addAnimation:basic forKey:@"scaleAnimation"];
}

@end
 1 /**
 2  *  缩放
 3  */
 4 - (void)scaleAnimation {
 5     
 6     CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
 7     
 8     basic.fromValue = @1;
 9     basic.toValue = @5;
10     
11     basic.duration = 1.0f;
12     
13     //开启动画回放
14     basic.autoreverses = YES;
15     
16     basic.repeatCount = 3;
17     basic.repeatDuration = 6;
18     basic.beginTime = CACurrentMediaTime() + 3;
19     
20     /*       kCAFillModeForwards //在动画结束播放之后,将图层保留结束为止
21              kCAFillModeBackwards //在动画没开始之前,将图层保留在起点位置
22              kCAFillModeBoth //上面两者集合
23              kCAFillModeRemoved //动画结束之后,移除动画效果,图层返回最初状态
24      */
25     basic.removedOnCompletion = YES;
26     basic.fillMode = kCAFillModeBoth;
27     
28     [self.imageView.layer addAnimation:basic forKey:@"scaleAnimation"];
29 }
时光见证了成长,还很无知,我想一点点幼稚转为有知!
原文地址:https://www.cnblogs.com/foreveriOS/p/5404428.html