我的应用程序我想将 UIImageView 从默认位置移动到用户点击的位置。但是,如果在完成此动画之前,用户再次点击其他点,则该 imageView 不应该从原始位置开始,而是应该从点击新点后停止动画的点开始。我怎样才能做到这一点。以下是将 imageView 移动到用户点击位置的代码。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=[[event allTouches]anyObject];
touchedPoint= [touch locationInView:touch.view];
[imageViews.layer removeAllAnimations];
[UIImageView animateWithDuration:2.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[CATransaction begin];
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 2.0f;
pathAnimation.calculationMode = kCAAnimationPaced;
animationPath = [UIBezierPath bezierPath];
[animationPath moveToPoint:imageViews.center];
[animationPath addLineToPoint:CGPointMake(touchedPoint.x,touchedPoint.y)];
pathAnimation.path = animationPath.CGPath;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
[imageViews.layer addAnimation:pathAnimation forKey:@"animatePosition"];
[CATransaction commit];
}
completion:nil]; //or completion:^(BOOL) finished { your code here for when the animation ended}];
}