//
// ViewController.m
#import "ViewController.h"
// 角度转弧度
#define angle2Radion(angle) (angle / 180.0 * M_PI)
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 创建动画
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";
// rotation旋转,需要添加弧度值
// angle2Radion() 角度转弧度的宏
// @() 包装为NSNumber对象
anim.values = @[@(angle2Radion(-5)),@(angle2Radion(5)),@(angle2Radion(-5))];
anim.repeatCount = MAXFLOAT;
// anim.duration = 1;
[_redView.layer addAnimation:anim forKey:nil];
}
@end
2.实现下面效果只需要修改控件的锚点
- (void)viewDidLoad {
[super viewDidLoad];
// 修改锚点
_redView.layer.anchorPoint = CGPointZero;
}
3.沿着一个路径做动画
// 创建动画
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
anim.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 100, 150, 150)].CGPath;
anim.repeatCount = MAXFLOAT;
anim.duration = 1;
[_redView.layer addAnimation:anim forKey:nil];