How to change speed during CABasicAnimation animation

In my application, I use CABasicAnimation for animation. I want to dynamically change the speed of the animation, so I added one slider to change the speed. Below is my animation code. But I can’t change the speed, when I change the speed value, 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;

   }
+5
source share
6 answers

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

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

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

+6

EDIT: , : , . . , jarring.

, , (. ), .

:

, . . . - :

CABasicAnimation *a = [displayLabel.layer animationForKey:@"rotationAnimation"];
a.duration = slider.value;
+2

, jrturton , . , , .

A D A-B, B-C, C-D. animationDidStop, , .

, , .

+1

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

but do not forget to write off fromValue and toValue and use the old toValue as the new fromValue to perform a smooth change

+1
source

Set the speed as what you need.

    a.duration=0.5;

Try it...

0
source

If you just want to use Autoscrolling text, you can also use one class.

http://blog.stormyprods.com/2009/10/simple-scrolling-uilabel-for-iphone.html

It may also work in your case, try.

0
source

All Articles