动画
<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>
这一有个问题 就是 会闪一下 没有上边那个完美 如果哪位大神知道怎么解决 还请不吝赐教