如何在动画CABasicAnimation时改变速度

Pre*_*van 6 cocoa-touch objective-c cabasicanimation ipad ios

在我的应用程序中,我使用CABasicAnimation进行动画制作.我想动态更改动画的速度,所以我添加了一个滑块来改变速度.以下是我的动画代码.但是我无法改变速度,当我改变速度值时没有任何反应.

        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;

   }
Run Code Online (Sandbox Code Playgroud)

小智 6

我认为改变速度的最佳方法是改变你的图层的时间系统

displayLabel.layer.timeOffset =
     [displayLabel.layer convertTime:CACurrentMediaTime() fromLayer:nil]; 
displayLabel.layer.beginTime = CACurrentMediaTime(); 
displayLabel.layer.speed= slider.value;
Run Code Online (Sandbox Code Playgroud)

你可以看到这个提前.https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreAnimation_guide/AdvancedAnimationTricks/AdvancedAnimationTricks.html#//apple_ref/doc/uid/TP40004514-CH8-SW2


Aya*_*yaz 0

根据需要设置速度。

    a.duration=0.5;
Run Code Online (Sandbox Code Playgroud)

尝试这个...