我的应用程序我想将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}];
}
答案 0 :(得分:1)
请改用:
[UIImageView animateWithDuration:2.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
} completion:nil];
UIViewAnimationOptionBeginFromCurrentState
选项将使动画从图像的当前位置开始。因此,如果您使用另一个动画移动图像,它将很好地融合在一起。
希望这有帮助。
干杯!
答案 1 :(得分:0)
如果动画在用户点击以更改其方向之前停止,则会使计时器失效。