IOS CALayer 动画

动画 

<span style="font-size:18px;">- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    CGPoint point = [touches.anyObject locationInView:self.view];

    UIView *btn = [self.view viewWithTag:-10];
    CABasicAnimation *patho = [CABasicAnimation animationWithKeyPath:@"position"];
    patho.toValue = [NSValue valueWithCGPoint:point];
    patho.duration = 3;

    [btn.layer addAnimation:patho forKey:@""];
}
</span>


这样写 有一个 问题就是 当动画执行结束以后,立刻就回到了原来的位置,所以要想解决这个问题可以这样设置

<span style="font-size:18px;">- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    CGPoint point = [touches.anyObject locationInView:self.view];

    UIView *btn = [self.view viewWithTag:-10];
    CABasicAnimation *patho = [CABasicAnimation animationWithKeyPath:@"position"];
    patho.toValue = [NSValue valueWithCGPoint:point];
    patho.duration = 3;
    patho.removedOnCompletion = false;
    patho.fillMode = kCAFillModeForwards;
    [btn.layer addAnimation:patho forKey:@""];
}</span>

上边 这两句必须都写上,否则还会回到原来的位置.

还有一种解决方法

<span style="font-size:18px;">- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    CGPoint point = [touches.anyObject locationInView:self.view];

    UIView *btn = [self.view viewWithTag:-10];
    CABasicAnimation *patho = [CABasicAnimation animationWithKeyPath:@"position"];
    patho.toValue = [NSValue valueWithCGPoint:point];
    patho.duration = 3;
    patho.delegate = self;
    [patho setValue:patho.toValue forKey:@"endPositon"];
    [btn.layer addAnimation:patho forKey:@""];
}

- (void)animationDidStart:(CAAnimation *)anim{
    
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if (flag) {
        CGPoint point = [[anim valueForKey:@"endPositon"] CGPointValue];
        UIView *btn = [self.view viewWithTag:-10];

        [CATransaction begin];
        [CATransaction setDisableActions:YES];
        btn.layer.position = point;
        [CATransaction commit];

    }
}</span>

这一有个问题 就是 会闪一下 没有上边那个完美 如果哪位大神知道怎么解决 还请不吝赐教


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值