Is there a way to get the state of a position (or center) from an unfinished UIView animation?

My UIView does a simple animation. He adjusts his position y from 100 to 130, and then changes direction. I want it to keep repeating, so I have a repeat counter set to 999. After user input, I want to take the same UIView and adjust the x position. This is done using another UIView animation. The problem is that when the second animation starts, the first animation (which goes from 100 to 130 in the y direction) just ends abruptly (since I read that it should). Is there a way to get the final position of the y coordinate of this UIView before it ends? Ideally, I would like the UIView to stay in the same y position it was in when I translated the x coordinates.

Summary: UIView moves in the y direction from 100-130, reverses and repeats until user input is received. After receiving, the animation is trimmed, and UIView goes to y = 130. I would like to know what the final y value is before the animation was interrupted, so when using a new animation using x-translation, UIView will not jump to 130, but will remain the same position in which he was when the 1st animation is completed.

I don’t seem to see anything that would allow you to do this. It seems to me that after you set the animation in motion using UIView, it (and all current state changes) got out of your hands and will only be β€œreturned” to your control and accessibility after the animation is completed, and to the designated end point. It's right? Any insight would be appreciated. Thank you for your time.

+4
source share
3 answers

You are looking for a "presentation layer".

Each UIView is rendered using the Core Animation layer , accessible from the UIView layer property.

CALayer has a presentationLayer method that returns a CALayer that represents a "close approximation to the version of the rendered layer."

So, to get the current position of your view:

 CGRect currentViewFrame = [[myView.layer presentationLayer] frame]; 
+9
source

I'm not sure if this is exactly what you want, but the UIView setAnimationBeginsFromCurrentState: used inside the beginAnimations block will cause the x animation to start where y is running.

0
source

Perhaps for what you are trying to accomplish, it would be helpful to have some control over the animation. For this task, I suggest using NSTimer, scheduled for any period of time that is best suited for you (from 1/30 to 1/60) and just adjust the position of the UIView every time the timer fires. This way you can always access the components X and Y of the view position.

Sort of:

 - (void)timerFired:(NSTimer *)timer { CGPoint p = view.center; if (py >= 130.0) { positiveMovement = NO; } else if (py <= 100.0) { positiveMovement = YES; } if (positiveMovement) p.y++; else py--; view.center = p; } 

positiveMovement will be just a BOOL instance variable. Then you can simply directly adjust the X value of the viewing position in another place with user input, and it will be updated accordingly.

0
source

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


All Articles