CAKeyframeAnimation的简单使用

本文介绍了一个具体的iOS应用中实现视图触控动画的过程。通过使用Core Animation中的CAKeyframeAnimation,文章展示了如何定义动画路径并调整动画效果,同时实现了动画的开始与结束代理方法。

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

@interface MJViewController ()

@property (weak, nonatomic) IBOutlet UIView *redView;


@end


@implementation MJViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];

    

    anim.keyPath = @"position";

    anim.removedOnCompletion = NO;

    anim.fillMode = kCAFillModeForwards;

    anim.duration = 2.0;

    

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));

    anim.path = path;

    CGPathRelease(path);

    

    // 设置动画的执行节奏

    // kCAMediaTimingFunctionEaseInEaseOut : 一开始比较慢, 中间会加速临近结束的时候, 会变慢

    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    anim.delegate = self;

    

    [self.redView.layer addAnimation:anim forKey:nil];

}


#pragma mark - 动画的代理方法

#pragma mark 动画开始的时候调用

- (void)animationDidStart:(CAAnimation *)anim

{

    NSLog(@"animationDidStart");

}

#pragma mark 动画结束的时候调用

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

{

    NSLog(@"animationDidStop");

}


- (void)testMove

{

    //    CABasicAnimation  fromValue --> toValue

    

    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];

    

    anim.keyPath = @"position";

    

    NSValue *v1 = [NSValue valueWithCGPoint:CGPointZero];

    NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(100, 0)];

    NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];

    NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(0, 200)];

    anim.values = @[v1, v2, v3, v4];

    

    //    anim.keyTimes = @[@(0.5), @(0.25), @(0.25)];

    

    anim.duration = 2.0;

    

    

    anim.removedOnCompletion = NO;

    anim.fillMode = kCAFillModeForwards;

    

    [self.redView.layer addAnimation:anim forKey:nil];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值