Change the current animation

Basically, what I'm trying to do is revitalize the cloud, and then change its speed and / or direction of the average animation if the wind changes. If that matters, I control it all from the UIViewController, and the cloud exists UIView with CALayer on top of this, which is the cloud. I also tried this with a UIImageView on top.

For TL: DR types, in short, what I'm trying to do is either get the position of the animated view, or stop the animated view using block animations.

And here is the full story. My problem is to get my position during the animation. I use block animation. Because I only know the speed with which he must move, I need to calculate the time myself, using the distance on the left. I tried the following code and several variations of it:

[[Cloud CloudImage]convertPoint:CGPointMake([[[Cloud CloudImage] presentationLayer] position].x, 0) toLayer:self.layer].x 

Where Cloud is UIView and CloudImage is CALayer. This was the most difficult variation I tried, I tried a few simpler ones (with a direct request to the Cloud, for example, or with a UIView instead of CALayer). However, all that it returns is its final value. I read something about this method, which has been broken since 3.2, but fixed in 4.2; However, this was not fixed when I changed the deployment target to iOS 4.3 instead of 4.0. I am using basic sdk 4.3.

Several other options that I considered stopped the animation for a while, and then immediately took a position and started a new animation. However, I will need to know how to stop the block-based animation, and I only found fragments of the old animation system (commitanimations).

The last one I reviewed was writing my own animation system; The cloud will have a repeating NSTimer after 0.08 seconds or so, and create a kernel animation of 0.08 seconds each time it starts, for which it uses the speed given to the cloud as a property. However, I am afraid that any version of this will have much lower performance, while I need it to be as light as possible, since I have up to 20 of these clouds at the same time (and sometimes with rain) .

Thanks in advance!

+4
source share
1 answer

In this case, I would definitely flip my own system, and performance losses compared to using the built-in animation code can be minimized using CADisplayLink rather than NSTimer ( CADisplayLink directly tied to screen updates timer).

The fact that you have 20 clouds at the same time does not change things too much, since I assume that you intend to animate these 20 clouds separately using the built-in animations.

If you are not sure how big a performance hit it will have on things in the end, you can try just adding a bunch of clouds (50 or so) using the built-in animation and see how they move slowly, then switching it to the built-in animation code and comparing.

Edit: in this discussion, Qaru describes in detail how to do what you ask if you go along this route: Cancel UIView animation?

Example:

 // startAnimating called once to initiate animation loop. might be stopped eg // if game is paused or such - (void)startAnimating { // displayLink = the CADisplayLink for the current animation, if any. if (displayLink) { [displayLink invalidate]; [displayLink release]; } displayLink = [[CADisplayLink displayLinkWithTarget:self selector:@selector(animationTick:)] retain]; [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; } // tick method is called at every interval; we want to update things based on // a delta time (duration), so that if things get bogged down and the updates // come less often, we don't go into slow motion - (void)tick:(CADisplayLink *)sender { CFTimeInterval duration = sender.duration; // here, we update the position for all the UIView objects. example: CGRect cloudFrame; for (UIView *cloud in clouds) { cloudFrame = cloud.frame; cloudFrame.origin.x += windForceX * duration; cloudFrame.origin.y += windForceY * duration; cloud.frame = cloudFrame; } // here we might update the windForceX and Y values or this might happen somewhere // else } 
+7
source

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


All Articles