关键帧动画CAKeyframeAnimation

demo 都是可以直接拿进去用的。


//
//  ViewController.m
//  关键帧动画CAKeyframeAnimation
//
//  Created by 陆巧怡 on 15/7/30.
//  Copyright (c) 2015年 Simon. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIView *customView;
@property (nonatomic, strong) UIButton *button;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view];
    
    
    
    
    
    self.customView = [[UIView alloc]initWithFrame:CGRectMake(75, 75, 50, 50)];
    self.customView.layer.backgroundColor = [UIColor redColor].CGColor;
    //self.customView.backgroundColor = [UIColor brownColor];
    [self.view addSubview:self.customView];
    
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.button setTitle:@"停止" forState:UIControlStateNormal];
    self.button.frame = CGRectMake(200, 200, 100, 20);
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
    
    
    
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
   
    //1.创建关键帧核心动画
    CAKeyframeAnimation *keyAnima = [CAKeyframeAnimation animation];
    
    //1.1 告诉系统执行什么动画
    keyAnima.keyPath = @"position";
    
    //创建路径
#warning C语言中,但凡带Create的都必须释放
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddEllipseInRect(path, NULL, CGRectMake(0, 100, 200, 200));
    
    
    keyAnima.path = path;
    CGPathRelease(path);
    
    
    //1.2 保存执行完之后的状态
    //1.2.1 执行完之后不删除动画
    keyAnima.removedOnCompletion = NO;
    //1.2.2 执行完之后保存最新的状态
    keyAnima.fillMode = kCAFillModeForwards;
    //1.3 设置动画时间
    keyAnima.duration = 5.0;
    
    //2.观察动画什么时候执行,以及什么时候执行完毕
    keyAnima.delegate = self;
    
    //3.添加核心动画
    [self.customView.layer addAnimation:keyAnima forKey:@"tingzhi"];
    
}

#warning 平移 路径是一个四边形 这个用的比较少
-(void)text1{
    
    //1.创建关键帧核心动画
    CAKeyframeAnimation *keyAnima = [CAKeyframeAnimation animation];
    
    //1.1 告诉系统执行什么动画
    keyAnima.keyPath = @"position";
    
    NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
    NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(200, 100)];
    NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
    NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(100 , 200)];
    NSValue *v5= [NSValue valueWithCGPoint:CGPointMake(100, 100)];
    
    keyAnima.values = @[v1,v2,v3,v4,v5];
    
    //这是一个时间点得问题 有几关键帧 那就是动画时间按照0~1去设置好。
    //keyAnima.keyTimes = @[@(0.08),@(0.09),@(0.1),@(0.11),@(1)];
    
    //设置动画节奏
    //keyAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    //1.2 保存执行完之后的状态
    //1.2.1 执行完之后不删除动画
    keyAnima.removedOnCompletion = NO;
    //1.2.2 执行完之后保存最新的状态
    keyAnima.fillMode = kCAFillModeForwards;
    //1.3 设置动画时间
    keyAnima.duration = 5.0;
    
    //2.观察动画什么时候执行,以及什么时候执行完毕
    keyAnima.delegate = self;
    
    //2.添加核心动画
    [self.customView.layer addAnimation:keyAnima forKey:@"position"];
    
}


-(void)animationDidStart:(CAAnimation *)anim{
    
    NSLog(@"动画开始");
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    
    NSLog(@"动画结束");

}

-(void)buttonAction:(UIButton *)btn{
    
    
    [self.customView.layer removeAnimationForKey:@"tingzhi"];
}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值