2

我的应用程序我想将 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}];

}
4

2 回答 2

1

改用这个:

[UIImageView animateWithDuration:2.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
} completion:nil];

UIViewAnimationOptionBeginFromCurrentState选项将使动画从图像的当前位置开始。因此,如果您使用另一个动画移动图像,它会很好地融合在一起。

希望这可以帮助。

干杯!

于 2012-07-10T06:55:15.403 回答
0
  1. 定义一个全局CGPoint;
  2. 沿着动画开始一个 0.1 秒的步长重复计时器(在用户第一次点击时)。
  3. 在每个滴答计数中,读取图像的位置并设置上面定义的 CGPoint。
  4. 动画开始后,当用户再次点击时,您会停止上一个动画,并启动另一个动画,使用上面的 CGPoint 作为“moveToPoint”。

如果动画在用户点击更改其方向之前停止,则使计时器无效。

于 2012-07-10T07:25:24.943 回答