Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change Speed while Animation CABasicAnimation

In my application i am using CABasicAnimation for animation. I want to change the speed of the animation dynamically so i have added one slider to change the speed. Following is my animation code. But i am not able to change the speed, when i change the value of speed nothing happens.

        CABasicAnimation * a = [CABasicAnimation animationWithKeyPath:@"position"];
    [a setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];

    CGPoint startPt = CGPointMake(self.view.bounds.size.width + displayLabel.bounds.size.width / 2,
                                  displayLabel.frame.origin.y);
    CGPoint endPt = CGPointMake(displayLabel.bounds.size.width / -2, displayLabel.frame.origin.y);

    [a setFromValue:[NSValue valueWithCGPoint:startPt]];
    [a setToValue:[NSValue valueWithCGPoint:endPt]];
    [a setAutoreverses:NO];
    [a setDuration:speeds];
    [a setRepeatCount:HUGE_VAL];
    [displayLabel.layer addAnimation:a forKey:@"rotationAnimation"];


    - (IBAction)speedSlider:(id)sender {

         speeds = slider.value;

   }
like image 339
Prerna chavan Avatar asked Jun 26 '12 10:06

Prerna chavan


People also ask

How to change the playback speed of the animation?

How to change your animation playback speed 1 Go to Window --> Settings/Preferences --> Preferences 2 On the list on the left, click 'Time Slider' 3 Change the option 'Playback Speed'

How to change the timing of an animation in Canva?

By default, Canva sets the timing per page to 5 seconds. To change that, all you need to do is (granting you’ve already added an animation style to the page): Step 1: Click once on the background of the canvas you’re currently working on. Step 2: Choose the watch icon on the editor toolbar’s left side.

Can I change the speed of a text animation?

Whether you select a text or a page animation, the actual speed of the animation itself can not truly be adjusted. But, you can alter the duration of a particular page or slide to give the illusion that you changed the animation speed — even if technically you didn’t.

How do I change the speed of my video design?

The second way is to directly type inside the box the speed that you want. Just click inside the box, type the numbers inside it, and press Enter on your keyboard to save the changes. Repeat these steps for all the pages on your video design.


4 Answers

I think the best way to change speed is change your layer's time system

displayLabel.layer.timeOffset =
     [displayLabel.layer convertTime:CACurrentMediaTime() fromLayer:nil]; 
displayLabel.layer.beginTime = CACurrentMediaTime(); 
displayLabel.layer.speed= slider.value;

You can see this for advance. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreAnimation_guide/AdvancedAnimationTricks/AdvancedAnimationTricks.html#//apple_ref/doc/uid/TP40004514-CH8-SW2

like image 167
Yu-Lin Wang Avatar answered Sep 22 '22 13:09

Yu-Lin Wang


EDIT: It looks like you will have a further problem, though: it doesn't look like you can change values like that on a running animation. You will have to remove the current animation and add a new one with the altered value. This may need a bit of care to prevent a jarring effect when you add the new animation.

From the thread above, you might be able to do this by not repeating the animation, but by using a delegate (see here) to keep re-adding the animation, and setting the new speed for the next animation cycle instead.

Original post:

You are changing the value that you had originally passed in to the animation. This isn't going to affect the running animation. You'll need to get a reference to that, and change the duration property of the animation object. Something like this in your action method:

CABasicAnimation *a = [displayLabel.layer animationForKey:@"rotationAnimation"];
a.duration = slider.value;
like image 39
jrturton Avatar answered Sep 19 '22 13:09

jrturton


I think jrturton is correct that you can't change properties on an animation that is already running. But what you could do is break the animation into short segments and change the speed for the next segment when the slider value changes.

Instead of animating from point A to point D, you'd animate from A-B, then B-C, then C-D. Use the parent class's animationDidStop to check the current point, check the slider value, and kick off the next animation.

This might produce jerky motion, but if you use very small segments, you might be able to smooth it out.

like image 23
Wienke Avatar answered Sep 22 '22 13:09

Wienke


u should stop the animation and restart a new with a new duration time

but remember to log down the fromValue and the toValue , and use the old toValue as the new fromValue to perform a seamless change

like image 20
chings228 Avatar answered Sep 21 '22 13:09

chings228