@interface ViewController ()
@property (nonatomic,strong) UIImageView *imageView
@property (nonatomic,strong) CAKeyframeAnimation *anim
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]
[self.view addSubview:self.imageView]
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.imageView.layer addAnimation:self.anim forKey:nil]
}
- (CAKeyframeAnimation *)anim {//可以根据路径做动画
if (!_anim) {
_anim = [CAKeyframeAnimation animation]
_anim.keyPath = @"transform.rotation"
_anim.values = @[@(angleToRadian(-5)),@(angleToRadian(5)),@(angleToRadian(-5))]
_anim.repeatCount = 5
_anim.autoreverses = YES
_anim.duration = 0.1
}
return _anim
}
///创建UIImageView
- (UIImageView *)imageView {
if (!_imageView) {
_imageView = [[UIImageView alloc] init]
_imageView.frame = CGRectMake(100, 150, 50, 50)
_imageView.contentMode = UIViewContentModeScaleAspectFit
_imageView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.6]
_imageView.image = [UIImage imageNamed:@"zrx4.jpg"]
_imageView.layer.cornerRadius = 5
_imageView.layer.masksToBounds = YES
}
return _imageView
}
@end
