码迷,mamicode.com
首页 > 其他好文 > 详细

核心动画(关键帧动画)-转

时间:2018-12-11 21:54:55      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:col   top   执行   super   核心   nbsp   sources   def   oval   

一.简单介绍 
是CAPropertyAnimation的子类,跟CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSArray保存这些数值。 

属性解析: 
values:就是上述的NSArray对象。里面的元素称为”关键帧”(keyframe)。动画对象会在指定的时间(duration)内,依次显示values数组中的每一个关键帧。 
path:可以设置一个CGPathRef\CGMutablePathRef,让层跟路径移动。path只对CALayer的anchorPoint和position起作用。如果你设置了path,那么values将被忽略 
keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧。当keyTimes没有设置的时候,各个关键帧的时间是平分的。 

说明: 
CABasicAnimation可看做最多只有2个关键帧的CAKeyframeAnimation。

 

二.代码示例 

第一种方式

@interface ViewController ()

@property(nonatomic,strong) UIView *customView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _customView = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                           100,
                                                           100,
                                                           100)];
    _customView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_customView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //创建核心动画
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animation];
    //平移
    keyAnimation.keyPath = @"position";
    //告诉系统要执行什么动画
    NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
    NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(200, 100)];
    NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
    NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
    NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
    keyAnimation.values = @[value1,value2,value3,value4,value5];
    //设置动画完毕后,不删除动画
    keyAnimation.removedOnCompletion = NO;
    //设置保持动画的最新状态
    keyAnimation.fillMode = kCAFillModeForwards;
    //设置动画执行的时间
    keyAnimation.duration = 4.0;
    //设置动画的节奏
    keyAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    //设置代理,开始-结束
    keyAnimation.delegate = self;
    //添加核心动画
    [_customView.layer addAnimation:keyAnimation forKey:nil];
}

#pragma mark Core Animation Delegate
- (void)animationDidStart:(CAAnimation *)anim{
    NSLog(@"开始动画");
}

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

@end

第二种方式(使用path)让layer在指定的路径上移动

@interface ViewController ()

@property(nonatomic,strong) UIView *customView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _customView = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                           100,
                                                           100,
                                                           100)];
    _customView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_customView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //创建核心动画
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animation];
    keyAnimation.keyPath = @"position";
    //告诉系统要执行什么动画
    //创建一条路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddEllipseInRect(path, NULL, CGRectMake(150, 100, 100, 100));
    keyAnimation.path = path;

    //有create就一定要有release
    CGPathRelease(path);

    //设置动画执行完毕后,不删除动画
    keyAnimation.removedOnCompletion = NO;
    //设置保存动画的最新状态
    keyAnimation.fillMode = kCAFillModeForwards;
    //设置动画执行的时间
    keyAnimation.duration = 5.0;
    //设置动画的节奏
    keyAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    //设置代理,开始-结束
    keyAnimation.delegate = self;
    //添加核心动画
    [_customView.layer addAnimation:keyAnimation forKey:nil];
}

#pragma mark Core Animation Delegate
- (void)animationDidStart:(CAAnimation *)anim{
    NSLog(@"开始动画");
}

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

@end

说明:可以通过path属性,让layer在指定的路径上运动。 
停止动画:

//添加核心动画
[self.customView.layer addAnimation:keyAnima forKey:@"wendingding"];
//停止self.customView.layer上名称标示为wendingding的动画
[self.customView.layer removeAnimationForKey:@"wendingding"];

三.图标抖动

#import "ViewController.h"

#define angle2Radian(angle)  ((angle)/180.0*M_PI)

@interface ViewController ()

@property(nonatomic,strong) UIImageView *iconView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _iconView = [[UIImageView alloc] initWithFrame:CGRectMake(160,
                                                              150,
                                                              100,
                                                              100)];
    _iconView.image = [UIImage imageNamed:@"logo_100.jpg"];
    [self.view addSubview:_iconView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //创建核心动画
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animation];
    keyAnimation.keyPath = @"transform.rotation";
    //设置动画时间
    keyAnimation.duration = 0.1;
    //把度数转换为弧度  度数/180*M_PI
    keyAnimation.values = @[@(-angle2Radian(4)),@(angle2Radian(4)),@(-angle2Radian(4))];
    //设置动画的重复次数(设置为最大值)
    keyAnimation.repeatCount = MAXFLOAT;

    keyAnimation.fillMode = kCAFillModeForwards;
    keyAnimation.removedOnCompletion = NO;
    //添加动画
    [self.iconView.layer addAnimation:keyAnimation
                               forKey:nil];
}
@end

 

核心动画(关键帧动画)-转

标签:col   top   执行   super   核心   nbsp   sources   def   oval   

原文地址:https://www.cnblogs.com/jiuyi/p/10105120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!