//
// ViewController.m
// 图标抖动
//
// Created by 陆巧怡 on 15/7/30.
// Copyright (c) 2015年 Simon. All rights reserved.
//
#import "ViewController.h"
#define angle2Radian(angle) ((angle)/180.0 * M_PI)
@interface ViewController ()
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
self.iconImageView.image = [UIImage imageNamed:@"newb"];
[self.view addSubview:self.iconImageView];
self.button = [UIButton buttonWithType:UIButtonTypeSystem];
[self.button setTitle:@"停止" forState:UIControlStateNormal];
self.button.frame = CGRectMake(200, 400, 100, 20);
[self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//创建核心动画
CAKeyframeAnimation *keyAnima =[CAKeyframeAnimation animation];
keyAnima.keyPath = @"transform.rotation";
//弧度 = 度数/180 * M_PI
keyAnima.values = @[@(-angle2Radian(4)),@(angle2Radian(4)),@(-angle2Radian(4))];
//设置动画重复的次数
keyAnima.repeatDuration = MAXFLOAT ;
//执行完不删除动画
keyAnima.removedOnCompletion = NO;
//保持动画的最新状态
keyAnima.fillMode = kCAFillModeForwards;
keyAnima.duration = 0.3;
//添加核心动画
[self.iconImageView.layer addAnimation:keyAnima forKey:@"tingzhi"];
}
-(void)buttonAction:(UIButton *)btn{
[self.iconImageView.layer removeAnimationForKey:@"tingzhi"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
图标抖动
最新推荐文章于 2021-06-02 09:29:14 发布