CoreAnimation使用

animation.gif
//
//  ViewController.m
//  雷达加弹出动画
//

#import "ViewController.h"

#define SCREEN_WIDTH self.view.bounds.size.width
#define SCREEN_HEIGHT self.view.bounds.size.height

#define ICONWIDTH 50
#define ICONDURATION 0.4

#define RGBA(r,g,b,a) [UIColor colorWithRed:(r)/255.f \
green:(g)/255.f \
blue:(b)/255.f \
alpha:(a)]

@interface ViewController ()<CAAnimationDelegate>
@property (strong, nonatomic) UIButton *button;
@property (strong, nonatomic) UIImageView *imgView1;
@property (strong, nonatomic) UIImageView *imgView2;
@property (strong, nonatomic) UIImageView *imgView3;
@property (strong, nonatomic) UIImageView *imgView4;
@property (strong, nonatomic) UIImageView *imgView5;
@property (nonatomic, strong) UIImageView *bottomView;
@property (nonatomic, strong) CALayer *aniLayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createRadarAnimation];
    [self createBottomView];
    [self createButtonAndIcons];
}

- (void)createRadarAnimation {
    
    UIView *aniView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.frame.size.height)];
    [self.view addSubview:aniView];
    aniView.backgroundColor = RGBA(253, 188, 54, 1);
    aniView.clipsToBounds = YES;
    
    CAShapeLayer *aniLayer = [CAShapeLayer layer];
    aniLayer.backgroundColor = RGBA(253, 222, 52, 1).CGColor;
    aniLayer.bounds = CGRectMake(0, 0, 60, 60);
    aniLayer.cornerRadius = 30;
    aniLayer.position = CGPointMake(SCREEN_WIDTH / 2, 360);
    
    //放大的动画
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = @(1);
    scaleAnimation.toValue = @(10);
    
    //透明度
    CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.values = @[@(0), @0.45, @0];
    opacityAnimation.keyTimes = @[@0, @(0.2), @1];
    
    //动画组
    CAAnimationGroup *aniGroup = [CAAnimationGroup animation];
    aniGroup.animations = @[scaleAnimation,opacityAnimation];
    aniGroup.duration = 2;
    aniGroup.repeatCount = CGFLOAT_MAX;
    
    [aniLayer addAnimation:aniGroup forKey:NSStringFromSelector(_cmd)];
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    [replicatorLayer addSublayer:aniLayer];
    replicatorLayer.instanceCount = 5;// 复制个数
    replicatorLayer.instanceDelay = 0.3;// 复制间隔
    [aniView.layer addSublayer:replicatorLayer];
    self.aniLayer = aniLayer;
}

- (void)createBottomView {
    self.bottomView = [[UIImageView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 200, SCREEN_WIDTH, SCREEN_HEIGHT - 80)];
    [_bottomView setImage:[UIImage imageNamed:@"bg"]];
    [_bottomView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:_bottomView];
}
- (void)createButtonAndIcons {
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(0, 0, 100, 100);
    self.button.center = CGPointMake(SCREEN_WIDTH / 2, 360);
    [self.button setBackgroundImage:[UIImage imageNamed:@"click"] forState:UIControlStateNormal];
    self.button.layer.cornerRadius = 50;
    self.button.layer.masksToBounds = YES;
    [self.button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.imgView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a"]];
    self.imgView1.frame = CGRectMake(0, 0, ICONWIDTH, ICONWIDTH);
    self.imgView1.center = self.button.center;
    
    self.imgView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a"]];
    self.imgView2.frame = CGRectMake(0, 0, ICONWIDTH, ICONWIDTH);
    self.imgView2.center = self.button.center;
    
    self.imgView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a"]];
    self.imgView3.frame = CGRectMake(0, 0, ICONWIDTH, ICONWIDTH);
    self.imgView3.center = self.button.center;
    
    self.imgView4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a"]];
    self.imgView4.frame = CGRectMake(0, 0, ICONWIDTH, ICONWIDTH);
    self.imgView4.center = self.button.center;
    
    self.imgView5 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a"]];
    self.imgView5.frame = CGRectMake(0, 0, ICONWIDTH, ICONWIDTH);
    self.imgView5.center = self.button.center;
    
    [self.view addSubview:_imgView1];
    [self.view addSubview:_imgView2];
    [self.view addSubview:_imgView3];
    [self.view addSubview:_imgView4];
    [self.view addSubview:_imgView5];
    [self.view addSubview:_button];
}

- (void)clickAction:(UIButton *)button {
    //停止雷达动画
    [self.aniLayer removeAllAnimations];
    
    //底部位移动画
    CABasicAnimation *positionAnimation = nil;
    positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    positionAnimation.duration = 2.0f;
    positionAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 40)];
    positionAnimation.removedOnCompletion = NO;
    positionAnimation.fillMode = kCAFillModeForwards;
    positionAnimation.delegate = self;
    [self.bottomView.layer addAnimation:positionAnimation forKey:NSStringFromSelector(_cmd)];
    
    //button关键帧动画
    CAKeyframeAnimation *keyAni = nil;
    keyAni = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    keyAni.duration = 2.0f;
    keyAni.values = @[@(1.5),@(1.0),@(1.5),@(1.0)];
    [button.layer addAnimation:keyAni forKey:NSStringFromSelector(_cmd)];
    
    //小图标位移动画
    CABasicAnimation *baseAni1 = nil;
    baseAni1 = [CABasicAnimation animationWithKeyPath:@"position"];
    baseAni1.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH / 2 - ICONWIDTH * 2 - 20, 120)];
    baseAni1.duration = ICONDURATION;
    baseAni1.removedOnCompletion = NO;
    baseAni1.fillMode = kCAFillModeForwards;
    [self.imgView1.layer addAnimation:baseAni1 forKey:NSStringFromSelector(_cmd)];
    
    CABasicAnimation *baseAni2 = nil;
    baseAni2 = [CABasicAnimation animationWithKeyPath:@"position"];
    baseAni2.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH / 2 - ICONWIDTH - 10, 120)];
    baseAni2.duration = ICONDURATION;
    baseAni2.beginTime = CACurrentMediaTime() + ICONDURATION;
    baseAni2.removedOnCompletion = NO;
    baseAni2.fillMode = kCAFillModeForwards;
    [self.imgView2.layer addAnimation:baseAni2 forKey:NSStringFromSelector(_cmd)];
    
    CABasicAnimation *baseAni3 = nil;
    baseAni3 = [CABasicAnimation animationWithKeyPath:@"position"];
    baseAni3.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH / 2, 120)];
    baseAni3.duration = ICONDURATION;
    baseAni3.beginTime = CACurrentMediaTime() + ICONDURATION * 2;
    baseAni3.removedOnCompletion = NO;
    baseAni3.fillMode = kCAFillModeForwards;
    [self.imgView3.layer addAnimation:baseAni3 forKey:NSStringFromSelector(_cmd)];
    
    CABasicAnimation *baseAni4 = nil;
    baseAni4 = [CABasicAnimation animationWithKeyPath:@"position"];
    baseAni4.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH / 2 + ICONWIDTH + 10, 120)];
    baseAni4.duration = ICONDURATION;
    baseAni4.beginTime = CACurrentMediaTime() + ICONDURATION * 3;
    baseAni4.removedOnCompletion = NO;
    baseAni4.fillMode = kCAFillModeForwards;
    [self.imgView4.layer addAnimation:baseAni4 forKey:NSStringFromSelector(_cmd)];
    
    CABasicAnimation *baseAni5 = nil;
    baseAni5 = [CABasicAnimation animationWithKeyPath:@"position"];
    baseAni5.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH / 2 + ICONWIDTH * 2 + 20, 120)];
    baseAni5.duration = ICONDURATION;
    baseAni5.beginTime = CACurrentMediaTime() + ICONDURATION * 4;
    baseAni5.removedOnCompletion = NO;
    baseAni5.fillMode = kCAFillModeForwards;
    [self.imgView5.layer addAnimation:baseAni5 forKey:NSStringFromSelector(_cmd)];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    self.button.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

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

推荐阅读更多精彩内容