Core Animation之组动画

1、CAAnimationGroup组动画

CAAnimationGroup组动画是CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行

属性解析:
animations:用来保存一组动画对象的NSArray
默认情况下,一组动画对象是同时运行的,也可以通过设置动画对象的beginTime属性来更改动画的开始时间

2、组动画代码

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong)  CALayer *redLayer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self.view.layer addSublayer:self.redLayer];
}

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

    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    // 缩放动画
    CABasicAnimation *basicTrans = [CABasicAnimation animation];
    
    basicTrans.keyPath = @"transform.scale";
    
    basicTrans.toValue = @(0.5);
    
    // 旋转动画
    CABasicAnimation *basicRotation = [CABasicAnimation animation];
    
    basicRotation.keyPath = @"transform.rotation";
    
    basicRotation.toValue = @(M_PI_2 * 30);
    
    
    // 关键帧平移动画
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animation];
    
    keyFrame.keyPath = @"position";
    
    keyFrame.path = ([UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 200, 320, 200)].CGPath);


    group.animations = @[basicTrans,basicRotation,keyFrame];
    
    group.repeatCount = MAXFLOAT;
    
    group.duration = 5.0;
    // 设置动画执行的速度
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    [self.redLayer addAnimation:group forKey:nil];
}
// 懒加载创建一个红色的图层
-(CALayer *)redLayer{
    if (_redLayer ==nil) {
        _redLayer = [CALayer layer];
        _redLayer.backgroundColor = [UIColor redColor].CGColor;
        _redLayer.bounds = CGRectMake(0, 0, 100, 100);
        _redLayer.position = CGPointMake(50, 50);
        self.redLayer = _redLayer;
    }
    return _redLayer;
}
@end

3、动画效果

Untitled.gif

4、组动画示例2

#import "ViewController.h"

@interface ViewController ()
{   // 位置序列数组
    NSMutableArray *sequencePositionArray;
    // 视图序列数组
    NSMutableArray *sequenceViewArray;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     // 创建数组对象
    sequencePositionArray = [NSMutableArray array];
    sequenceViewArray = [NSMutableArray array];
    // 颜色数组 来设置views的颜色
    NSArray *colorArray = @[[UIColor blueColor],[UIColor blackColor],[UIColor darkGrayColor],[UIColor redColor],[UIColor greenColor],[UIColor yellowColor],[UIColor purpleColor],[UIColor orangeColor]];
    // 获取控制器视图的终点坐标值
    CGFloat centerX = self.view.center.x;
    
    CGFloat centerY = self.view.center.y;
    // 设置一个四分之一弧度
    CGFloat delta = 2*M_PI/8;
    // 创建八个视图以及对应的位置
    for (int i =0; i<8; i++) {
        CGFloat x = centerX + 50 * cos(delta*i);
        CGFloat y = centerY + 50 * sin(delta*i);
        [sequencePositionArray addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        view.center = self.view.center;
        view.backgroundColor = colorArray[i];
        view.layer.cornerRadius = 5;
        view.layer.transform = CATransform3DMakeScale(0, 0, 0);
        view.layer.masksToBounds = YES;
        [self.view addSubview:view];
        
        [sequenceViewArray addObject:view];
    }

}
// 创建组动画 封装在一个方法中
- (CAAnimationGroup *)createGroupAnimationWithDelay:(CFTimeInterval)time withIndex:(NSInteger)index;
{
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimation.fromValue = [NSValue valueWithCGPoint:self.view.center];
    moveAnimation.toValue = sequencePositionArray[index];
    // moveAnimation.beginTime = 0; 平移动画的开始时间为0
    moveAnimation.duration = 0.75;
    moveAnimation.autoreverses = YES; // 允许动画自动反转
    
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = @(0);
    scaleAnimation.toValue = @(1);
    scaleAnimation.duration = 0.75;
    scaleAnimation.autoreverses = YES;
    
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[moveAnimation,scaleAnimation];
    group.autoreverses = YES;
    group.duration = 0.75;
    group.repeatCount = HUGE_VALF; // 动画重复的次数最大值
    group.beginTime = CACurrentMediaTime()+time;
    return group;
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 给每一个动画开始的时间设置与自己下标对应的延迟时间 i * 0.1
    for (int i = 0;i<sequenceViewArray.count; i++) {
    UIView *view = sequenceViewArray[i];
    [view.layer addAnimation:[self createGroupAnimationWithDelay:i*0.1 withIndex:i] forKey:@"group"];
}
}
@end

5、效果图

Untitled.gif

6、组动画示例3

#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)  UIView *view1;
@property (nonatomic,strong)  UIView *view2;
@property (nonatomic,strong)  CAAnimationGroup *group;
@property (nonatomic,strong)  CAAnimationGroup *group1;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect RECT = CGRectMake(0, 0, 190, 145);
    _view1 = [[UIView alloc] initWithFrame:RECT];
    _view1.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height*0.45);
    _view1.backgroundColor = [UIColor redColor];
    _view1.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"123"].CGImage);
    _view2 = [[UIView alloc] initWithFrame:RECT];
    _view2.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height*0.45);
    _view2.backgroundColor = [UIColor greenColor];
    _view2.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"456"].CGImage);
    CABasicAnimation *zPosition = [CABasicAnimation animation];
    zPosition.keyPath = @"zPosition";
    zPosition.fromValue = @-1;
    zPosition.toValue = @1;
    zPosition.duration = 1.2;
    
    CABasicAnimation *zPosition1 = [CABasicAnimation animation];
    zPosition1.keyPath = @"zPosition";
    zPosition1.fromValue = @1;
    zPosition1.toValue = @-1;
    zPosition1.duration = 1.2;
    
    CAKeyframeAnimation *rotation = [CAKeyframeAnimation animation];
    rotation.keyPath = @"transform.rotation";
    rotation.values = @[ @0, @0.14, @0 ];
    rotation.duration = 1.2;
    rotation.timingFunctions = @[
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                 ];
    
    CAKeyframeAnimation *rotation1 = [CAKeyframeAnimation animation];
    rotation1.keyPath = @"transform.rotation";
    rotation1.values = @[ @0, @-0.14, @0 ];
    rotation1.duration = 1.2;
    rotation1.timingFunctions = @[
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                  ];
    
    CAKeyframeAnimation *position = [CAKeyframeAnimation animation];
    position.keyPath = @"position";
    position.values = @[
                        [NSValue valueWithCGPoint:CGPointZero],
                        [NSValue valueWithCGPoint:CGPointMake(110, -20)],
                        [NSValue valueWithCGPoint:CGPointZero]
                        ];
    position.timingFunctions = @[
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                 ];
    position.additive = YES;
    position.duration = 1.2;
    
    CAKeyframeAnimation *position1 = [CAKeyframeAnimation animation];
    position1.keyPath = @"position";
    position1.values = @[
                         [NSValue valueWithCGPoint:CGPointZero],
                         [NSValue valueWithCGPoint:CGPointMake(-110, -20)],
                         [NSValue valueWithCGPoint:CGPointZero]
                         ];
    position1.timingFunctions = @[
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]
                                  ];
    position1.additive = YES;
    position1.duration = 1.2;
    
    _group = [[CAAnimationGroup alloc] init];
    _group.animations = @[ zPosition, rotation, position ];
    _group.duration = 1.2;
    _group.repeatCount = MAXFLOAT;
    
    _group1 = [[CAAnimationGroup alloc] init];
    _group1.animations = @[ zPosition1, rotation1, position1 ];
    _group1.duration = 1.2;
    _group1.repeatCount = MAXFLOAT;
    [_view1.layer addAnimation:_group1 forKey:@"123"];
    [_view2.layer addAnimation:_group forKey:@"456"];
    
    [self.view addSubview:_view1];
    [self.view addSubview:_view2];
}
@end

7、运行效果

Untitled.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,921评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,635评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,393评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,836评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,833评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,685评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,043评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,694评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,671评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,670评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,779评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,424评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,027评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,984评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,214评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,108评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,517评论 2 343

推荐阅读更多精彩内容