关键帧动画

本文介绍了一个iOS应用中的动画实现案例,包括关键帧动画的创建及触发方式。通过触摸事件启动动画,利用Core Animation实现视图沿特定路径移动的效果,并展示了如何为视图添加摇晃动画。

#import "CZViewController.h"

@interface CZViewController ()
@property (nonatomic, strong) UIView *myView;
@end

@implementation CZViewController

- (
UIView *)myView
{
   
if (_myView == nil) {
       
_myView = [[UIView alloc] initWithFrame:CGRectMake(110, 100, 100, 100)];
       
_myView.backgroundColor = [UIColor redColor];
       
        [
self.view addSubview:_myView];
    }
   
return _myView;
}

- (
void)viewDidLoad
{
    [
super viewDidLoad];

    [
self myView];
}

//点击屏幕时调用
- (
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [
self positionAnim1];
}

#pragma mark - 关键帧动画
/** 摇晃动画 */
- (
void)shake
{
   
if ([self.myView.layer animationForKey:@"shake"]) return;
   
   
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
   
    anim.
values = @[@(-M_PI / 36), @(M_PI / 36), @(-M_PI / 36)];
//    anim.duration = 0.5f;
    anim.
repeatCount = MAXFLOAT;
   
    [
self.myView.layer addAnimation:anim forKey:@"shake"];
}

// 图层在指定的路径上运动
- (
void)positionAnim1
{
   
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
   
//绘制圆形路径
    anim.
path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(60, 100, 200, 200)].CGPath;
   
//持续时间
    anim.
duration = 2.0f;
    [
self.myView.layer addAnimation:anim forKey:nil];
}

// 在指定的几个点上运动
- (
void)positionAnim
{
   
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
   
   
NSValue *p1 = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
   
NSValue *p2 = [NSValue valueWithCGPoint:CGPointMake(50, 300)];
   
NSValue *p3 = [NSValue valueWithCGPoint:CGPointMake(250, 300)];
   
NSValue *p4 = [NSValue valueWithCGPoint:CGPointMake(250, 50)];
   
NSValue *p5 = [NSValue valueWithCGPoint:CGPointMake(50, 50)];

    anim.
values = @[p1, p2, p3, p4, p5];
   
// 图层会保持显示动画执行后的状态
   
// 设置保存动画的最新状态
    anim.
fillMode = kCAFillModeForwards;
   
// 设置动画执行完毕之后不删除动画
    anim.
removedOnCompletion = NO;
   
    anim.
duration = 2.0f;
   
    [
self.myView.layer addAnimation:anim forKey:nil];
}



@end
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值