iOS动画

CAAnimationGroup—动画组

2020-09-28  本文已影响0人  Silence_xl

一、简介

image
#define Color(r, g, b, a)  ([UIColor colorWithRed:(r) / 255.0 green:(g) / 255.0 blue:(b) / 255.0 alpha:(a)])
#define RandColor Color(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255),1)

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) UIView *redView;
@property (strong, nonatomic) UIView *orangView;
@property (strong, nonatomic) UIView *purpleView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    [self.view addSubview:self.redView];
    [self.view addSubview:self.orangView];
    [self.view addSubview:self.purpleView];
}

- (UIView *)redView {
    if (!_redView) {
        _redView = [[UIView alloc] init];
        _redView.backgroundColor = [UIColor redColor];
        _redView.frame = CGRectMake(100, 100, 100, 100);
    }
    return _redView;
}

- (UIView *)orangView {
    if (!_orangView) {
        _orangView = [[UIView alloc] init];
        _orangView.backgroundColor = [UIColor orangeColor];
        _orangView.frame = CGRectMake(100, 250, 100, 100);
    }
    return _orangView;
}

- (UIView *)purpleView {
    if (!_purpleView) {
        _purpleView = [[UIView alloc] init];
        _purpleView.backgroundColor = [UIColor purpleColor];
        _purpleView.frame = CGRectMake(100, 400, 100, 100);
    }
    return _purpleView;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CAAnimationGroup *group1 = [CAAnimationGroup animation];///第一个组动画
    CAAnimationGroup *group2 = [CAAnimationGroup animation];///第二个组动画
    CAAnimationGroup *group3 = [CAAnimationGroup animation];///第三个组动画
    
    ///==============================第一个组动画=============================
    // 平移
    CABasicAnimation *group1_anim = [CABasicAnimation animation];
    group1_anim.keyPath = @"position";
    group1_anim.toValue = [NSValue valueWithCGPoint:CGPointMake(arc4random_uniform(200), arc4random_uniform(500))];
    // 缩放
    CABasicAnimation *group1_anim1 = [CABasicAnimation animation];
    group1_anim1.keyPath = @"transform.scale";
    // 0 ~ 1
    static CGFloat scale1 = 0.1;
    if (scale1 < 1) {
        scale1 = 1.5;
    }else{
        scale1 = 0.2;
    }
    group1_anim1.toValue = @(scale1);
    // 旋转
    CABasicAnimation *group1_anim2 = [CABasicAnimation animation];
    group1_anim2.keyPath = @"transform.rotation";
    group1_anim2.toValue = @(arc4random_uniform(360) / 180.0 * M_PI);
    group1.animations = @[group1_anim,group1_anim1,group1_anim2];
    group1.duration = 0.5;
    
    // 取消反弹
    // 告诉在动画结束的时候不要移除
    group1.removedOnCompletion = NO;
    // 始终保持最新的效果
    group1.fillMode = kCAFillModeForwards;
    ///==============================第一个组动画=============================
    
    
    ///==============================第二个组动画=============================
    // 平移
    CABasicAnimation *group2_anim = [CABasicAnimation animation];
    group2_anim.keyPath = @"position";
    group2_anim.toValue = [NSValue valueWithCGPoint:CGPointMake(arc4random_uniform(200), arc4random_uniform(500))];
    // 缩放
    CABasicAnimation *group2_anim1 = [CABasicAnimation animation];
    group2_anim1.keyPath = @"transform.scale";
    // 0 ~ 1
    static CGFloat scale2 = 0.1;
    if (scale2 < 1) {
        scale2 = 1.5;
    }else{
        scale2 = 0.2;
    }
    group1_anim1.toValue = @(scale2);
    // 旋转
    CABasicAnimation *group2_anim2 = [CABasicAnimation animation];
    group2_anim2.keyPath = @"transform.rotation";
    group2_anim2.toValue = @(arc4random_uniform(360) / 180.0 * M_PI);
    group2.animations = @[group2_anim,group2_anim1,group2_anim2];
    group2.duration = 0.5;
    // 取消反弹
    // 告诉在动画结束的时候不要移除
    group2.removedOnCompletion = NO;
    // 始终保持最新的效果
    group2.fillMode = kCAFillModeForwards;
    ///==============================第二个组动画=============================
    
    
    ///==============================第三个组动画=============================
    // 平移
    CABasicAnimation *group3_anim = [CABasicAnimation animation];
    group3_anim.keyPath = @"position";
    group3_anim.toValue = [NSValue valueWithCGPoint:CGPointMake(arc4random_uniform(200), arc4random_uniform(500))];
    // 缩放
    CABasicAnimation *group3_anim1 = [CABasicAnimation animation];
    group3_anim1.keyPath = @"transform.scale";
    // 0 ~ 1
    static CGFloat scale3 = 0.1;
    if (scale3 < 1) {
        scale3 = 1.5;
    }else{
        scale3 = 0.2;
    }
    group3_anim1.toValue = @(scale3);
    // 旋转
    CABasicAnimation *group3_anim2 = [CABasicAnimation animation];
    group3_anim2.keyPath = @"transform.rotation";
    group3_anim2.toValue = @(arc4random_uniform(360) / 180.0 * M_PI);
    group3.animations = @[group3_anim,group3_anim1,group3_anim2];
    group3.duration = 0.5;
    // 取消反弹
    // 告诉在动画结束的时候不要移除
    group3.removedOnCompletion = NO;
    // 始终保持最新的效果
    group3.fillMode = kCAFillModeForwards;
    ///==============================第一个组动画=============================
    
    _redView.backgroundColor = RandColor;
    _orangView.backgroundColor = RandColor;
    _purpleView.backgroundColor = RandColor;
    [_redView.layer addAnimation:group1 forKey:nil];
    [_orangView.layer addAnimation:group2 forKey:nil];
    [_purpleView.layer addAnimation:group3 forKey:nil];
}

@end

Simulator Screen Shot - iPhone 11 Pro Max - 2020-09-28 at 14.16.52.png
上一篇 下一篇

猜你喜欢

热点阅读