Reputation: 381
Using this code I'm trying to rotate an image. Image rotated but with a same speed. I'm trying to rotate an image with smooth rotation but I can't do this. Here is my code:
(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"];
}
Upvotes: 1
Views: 406
Reputation: 1661
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;
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;
Upvotes: 1