1

使用此代码我正在尝试旋转图像。图像旋转但速度相同。我正在尝试通过平滑旋转来旋转图像,但我不能这样做。这是我的代码:

(void)spinLayer:(CALayer *)inLayer duration:(CFTimeInterval)inDuration
    direction:(int)direction repeat:(int)repeat
{
    repeatcount = repeat+repeatcount;
    timer = [NSTimer scheduledTimerWithTimeInterval: inDuration target: self     selector:@selector(setCountLableValue) userInfo: nil repeats: YES];
    CABasicAnimation* rotationAnimation;
    rotationAnimation =
    [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.fromValue = [NSNumber numberWithFloat: M_PI *2.0 * direction];
    rotationAnimation.repeatCount = repeat;
    NSLog(@"Repeat count %d",repeat);
    rotationAnimation.duration = inDuration;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [inLayer addAnimation:rotationAnimation forKey:@"rotation"];

}
4

1 回答 1

1

There are different timingFunctions you can use. You used a linear function, try one of these instead of kCAMediaTimingFunctionLinear

NSString * const kCAMediaTimingFunctionEaseIn;
NSString * const kCAMediaTimingFunctionEaseOut;
NSString * const kCAMediaTimingFunctionEaseInEaseOut;

See https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CAMediaTimingFunction_class/Introduction/Introduction.html

Regarding the repeating problem, try adding the following code after creating the animation: (this is just an idea, I really don't know why the ease function is only used once)

rotationAnimation.removedOnCompleted = NO;
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.cumulative = YES;
于 2012-12-13T12:47:30.923 回答