//
// 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];
视图动画实践
本文介绍如何使用Objective-C在iOS应用中实现视图动画效果,包括通过角度到弧度的转换让视图旋转,并沿特定路径移动。此外还讨论了通过调整锚点来优化动画表现的方法。
562

被折叠的 条评论
为什么被折叠?



