核心动画

//
//  ViewController.m
//  core
//
//  Created by chenvinci on 2017/2/16.
//  Copyright © 2017年 cuijing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UIImageView *img;

@property(nonatomic) int iIndex;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.iIndex = 0;
    
    self.redView.layer.anchorPoint = CGPointMake(0, 0);
    self.redView.layer.position = CGPointMake(100, 100);

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //平移动画
    CABasicAnimation * moveA = [[CABasicAnimation alloc]init];
    moveA.keyPath = @"position";
    moveA.byValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)];//每次移动(50,50)
    
    //bounds动画
    CABasicAnimation * boundA = [CABasicAnimation animation];
    boundA.keyPath = @"bounds";
    boundA.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 40, 40)];
    
    //创建组动画
    CAAnimationGroup*group = [CAAnimationGroup animation];
    
    group.animations = @[moveA,boundA];
    group.duration = 3;
    
    //动画完成时不动
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    
    //添加动画
    [self.img.layer addAnimation:group forKey:nil];
}

-(void)touchesBegan3:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //创建转场动画
    CATransition* animation1 = [CATransition animation];

    NSArray* arr = @[@"cube",@"moveIn",@"reveal",@"pageCurl",@"suckEffect",@"rippleEffect",@"oglFlip"];
    
    if (self.iIndex >= arr.count) {
        self.iIndex = 0;
    }
    
    animation1.type = arr[self.iIndex];
    animation1.subtype = @"fromTop";
    
    self.iIndex++;
    
    
    
    //添加动画
    [self.img.layer addAnimation:animation1 forKey:nil];
}

-(void)touchesBegan2:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //创建关键帧动画
    CAKeyframeAnimation*animation1 = [[CAKeyframeAnimation alloc]init];
    
    animation1.duration = 4;
    
    //设置动画属性  修改position
    animation1.keyPath = @"position";
    
    NSValue*v1 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
    NSValue*v2 = [NSValue valueWithCGPoint:CGPointMake(100, 0)];
    NSValue*v3 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
    NSValue*v4 = [NSValue valueWithCGPoint:CGPointMake(0, 100)];
    NSValue*v5 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
    
    animation1.values = @[v1,v2,v3,v4,v5];
    
    //设置各关键帧的时间
    NSNumber*t1 = [NSNumber numberWithFloat:0.1];//最大是1
    NSNumber*t2 = [NSNumber numberWithFloat:0.2];
    NSNumber*t3 = [NSNumber numberWithFloat:0.8];
    NSNumber*t4 = [NSNumber numberWithFloat:0.9];
    NSNumber*t5 = [NSNumber numberWithFloat:1.0];
    
    animation1.keyTimes = @[t1,t2,t3,t4,t5];
    
    //动画执行完以后是否移除?
    animation1.removedOnCompletion = NO;
    animation1.fillMode = kCAFillModeForwards;//保持最新状态
    
    
    // 添加动画
    [self.redView.layer addAnimation:animation1 forKey:nil];
}

-(void)touchesBegan1:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //创建基础动画
    CABasicAnimation* animation1 = [[CABasicAnimation alloc]init];
    
    //设置动画属性  修改bounds
    animation1.keyPath = @"bounds";//改bounds属性要去掉autolayout
    animation1.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)];
    animation1.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
    
    
    //设置动画属性  修改position
    animation1.keyPath = @"position";
    animation1.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
    animation1.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
    
    
    //设置动画属性  修改transform
    animation1.keyPath = @"transform.translation.x";
    animation1.fromValue = [NSNumber numberWithInt:20];
    animation1.toValue = [NSNumber numberWithInt:50];
    
    
    //设置代理
    animation1.delegate = self;
    
    //设置时长
    animation1.duration = 2;
    
    //动画执行完以后是否移除?
    animation1.removedOnCompletion = NO;
    animation1.fillMode = kCAFillModeForwards;//保持最新状态
    // 添加动画
    [self.redView.layer addAnimation:animation1 forKey:nil];
    
    
}

- (void)animationDidStart:(CAAnimation *)anim{
    NSLog(@"start");
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    NSLog(@"stop");
}


@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容