CAKeyframeAnimation实现抖动效果

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  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];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值