UI进阶--Core Animation的简单使用:仿车小弟效果

示例布局和资源:

示例代码:

 1 //
 2 //  ViewController.m
 3 //  CarsAnimation
 4 //
 5 //  Created by xiaomoge on 15/1/6.
 6 //  Copyright (c) 2015年 xiaomoge. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 
11 @interface ViewController ()
12 @property (weak, nonatomic) IBOutlet UIImageView *imgView;
13 
14 @end
15 
16 @implementation ViewController
17 
18 - (void)viewDidLoad {
19     [super viewDidLoad];
20     //添加三个扇形按钮
21     for (int i = 0; i < 3; i++) {
22         UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
23         NSString *imgName = [NSString stringWithFormat:@"circle%d",i + 1];
24         [btn setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
25         btn.frame = self.imgView.bounds;
26         [self.imgView addSubview:btn];
27     }
28     //添加中心按钮
29     UIButton *centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
30     [centerBtn setBackgroundImage:[UIImage imageNamed:@"home_btn_dealer_had_bind"] forState:UIControlStateNormal];
31     centerBtn.frame = CGRectMake(0, 0, 112, 112);
32     centerBtn.center = self.imgView.center;
33     //注册中心按钮点击事件
34     [centerBtn addTarget:self action:@selector(centerClick) forControlEvents:UIControlEventTouchUpInside];
35     [self.view addSubview:centerBtn];
36 }
37 - (void)centerClick {
38     //取得当前的透明度
39     CGFloat currentAlpha = self.imgView.alpha;
40     if (currentAlpha == 1) {
41         self.imgView.alpha = 0;
42     }else {
43         self.imgView.alpha = 1;
44     }
45     //创建一个组动画
46     CAAnimationGroup *groupAni = [CAAnimationGroup animation];
47     //创建一个透明度动画
48     CABasicAnimation *opacityAni = [CABasicAnimation animationWithKeyPath:@"opacity"];
49     //创建一个缩放动画
50     CAKeyframeAnimation *scaleAni = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
51     //旋转动画
52     CABasicAnimation *rotationAni = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
53     
54     if (currentAlpha == 1) {//当显示的时候
55         opacityAni.fromValue = @1;//透明度从1转到0
56         opacityAni.toValue = @0;
57         
58         scaleAni.values = @[@1,@1.2,@0];//缩放先增大,后减小
59         rotationAni.fromValue = @0;
60         rotationAni.toValue = @(-M_PI_4);//旋转的角度从0到-45度,逆时针
61     }else {//当隐藏的时候
62         opacityAni.fromValue = @0;//透明度从1转到1
63         opacityAni.toValue = @1;
64         
65         scaleAni.values = @[@0,@1.2,@1];
66         rotationAni.fromValue = @(-M_PI_4);//旋转的角度
67         rotationAni.toValue = @0;
68     }
69     //动画的时间
70     groupAni.duration = 5;
71     //把透明度动画、缩放动画、旋转动画加到组动画中
72     groupAni.animations = @[opacityAni,scaleAni,rotationAni];
73     //把组动画加到图层中
74     [self.imgView.layer addAnimation:groupAni forKey:Nil];
75 }
76 @end
原文地址:https://www.cnblogs.com/xiaomoge/p/4208339.html