Problems with UIView, animateWithDuration and beginFromCurrentState

I have a custom view that should track the location of a user. I put the following code in touchesBegan as well as in touchesMoved :

 [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ cursorView.center = locationOfTouch; } completion:^(BOOL finished){}]; 

It seems pretty simple to me. I expect that the view will always animate the user's current location, even if that location is changed, and the view is still animating (due to the beginFromCurrentState option).

However, each animation is fully completed. They do not "switch" to a new animation, they do not end first, and then begin a new animation.

I tried adding this line to touchesMoved :

 [cursorView.layer removeAllAnimations]; 

Doing nothing. No animation is canceled. Any ideas?

+2
source share
3 answers

with [cursorView.layer removeAllAnimations]; you get access to the presentation layer, but you didn’t set the animation on the view directly, but on the view.
Try:

 [UIView beginAnimations:nil context:NULL] [UIView setAnimationDuration:0.2]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; cursorView.center = locationOfTouch; [UIView commitAnimations]; 

and leave removeAllAnimations and it should go from the current state.

+2
source

It seems like this is a derivative of the other question you asked, I just answered, but the same idea applies.

You do not need to use blocks for this. Just wrap your setCenter in a UIView animation. Like this:

 [UIView beginAnimations:nil context:NULL] [UIView setAnimationDuration:0.2f]; cursorView.center = locationOfTouch; [UIView commitAnimations]; 

Keep in mind that 0.2 seconds is pretty fast. It may seem that he is "jumping" into position. To confirm, set the duration to about 2.0 seconds and see if it comes to life.

Sincerely.

0
source

Technically, the view should automatically animate with a default time of 0.25 seconds. This is what the guides say, however, I'm here right now because the guides do not seem to be very accurate regarding animation.

Also, the beginAnimations call fades out, you can use CATransaction instead

0
source

Source: https://habr.com/ru/post/915137/


All Articles