Question

I have two images mosha1 & mosha2. i am moving them from a certain position to another point (206,89) through UIView animation. Now I want if the user touch any image while moving, the moving of that image will stop right there. So I set the code for stopping the animation movement:

[mosha1.layer removeAllAnimations];

This method calling stops the moving of the image, but however it is shown in the point (206,89) then. But it should appear in the exact position where it was touched by the user. How to solve this?

UIView animation method:

-(void)moshaMoveUp: (UIImageView *)mosha {

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.0];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
mosha.center = CGPointMake(206, 89);
[UIView commitAnimations];

}
Was it helpful?

Solution

The reason for this is how animation works with different layers. The moment you start the animation, your object logically is at the destination immediately. However there is the presentation layer that shows the object moving towards the destination. Hence if you want to stop the object, you need to get its current position from the presentation layer of the UIView, assign that to the view's positions and then remove the animation and your object should be in the right place.

OTHER TIPS

Once you stop the animation, the frame will be the final destination. If you want to stop it where it is, you have to get the view's layer's presentationLayer (which contains the current state of the object, not the final state), and use that to adjust the frame of view in question:

CALayer *layer = self.animatedView.layer.presentationLayer;
[self.animatedView.layer removeAllAnimations];
self.animatedView.frame = layer.frame;

Use this instead. What you are doing is very old and no longer supported.

[UIView animateWithDuration:3.0
                      delay:0.0
                    options:UIViewAnimationOptionsCurveEaseInOut
                 animations:^() {
                             mosha.center = CGPointMake(206, 89);
                            }
                 completion:nil];

This may or may not fix your cancellation problem but you need to use this method of animation.

I know this doesn't fix the problem. But it is still important to be using up to date and non-deprecated methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top