首页 > 代码库 > 核心动画-图标抖动
核心动画-图标抖动
//
// MJViewController.m
// 07-核心动画03-图标抖动
//
// Created by apple on 14-4-21.
// Copyright (c) 2014年 itcast. All rights reserved.
//
// 角度转弧度
#define Angle2Radian(angle) ((angle) / 180.0 * M_PI)
#import "MJViewController.h"
@interface MJViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
- (IBAction)start;
- (IBAction)stop;
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
/**
* 开始抖动
*/
- (IBAction)start {
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";
// 抖动角度
anim.values = @[@(Angle2Radian(-5)), @(Angle2Radian(5)), @(Angle2Radian(-5))];
// 动画执行时间
anim.duration = 0.25;
// 动画的重复执行次数 MAXFLOAT无限次
anim.repeatCount = MAXFLOAT;
// 保持动画执行完毕后的状态
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
// 动画标志,用于下面停止抖动
[self.iconView.layer addAnimation:anim forKey:@"shake"];
}
/**
* 停止抖动
*/
- (IBAction)stop {
[self.iconView.layer removeAnimationForKey:@"shake"];
}
@end
核心动画-图标抖动