Search code examples
ioscore-animationcabasicanimationcatransaction

unable to slow down animation speed in ios


I want to slow down animation speed when it is going to end.
I am going through this code.

[CATransaction begin];
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.byValue = [NSNumber numberWithFloat:20];
rotationAnimation.duration = 2;
rotationAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[CATransaction setCompletionBlock:^{
    imageView.transform = CGAffineTransformRotate(imageView.transform, DEGREES_TO_RADIANS(myAngle*32.72));
}];

[imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[CATransaction commit];

But unable to slow down when it is going to end or in completion block.


Solution

  • As i mention in @rakeshbs 's comment I want to stop animation at specific angle so I modified this code by following.

    [CATransaction begin];
    CABasicAnimation *rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.byValue = [NSNumber numberWithFloat:20];
    rotationAnimation.duration = 2;
    rotationAnimation.fromValue = 0;
    rotationAnimation.toValue = [NSNumber numberWithFloat:SomeValue];
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.fillMode = kCAFillModeForwards;
    [ImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    [CATransaction commit];
    

    by this code i'm able to stop at specific position via fromValue and controlling speed of starting and ending via [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    At the middle of animation if I want to increase the speed of animation I need to increase value of rotationAnimation.byValue and if decrease the speed I need to decrease value of same.

    Here I would like to mention that via this method if i want to rotate MyImageView at once for 360 degree then I need to set rotationAnimation.toValue = [NSNumber numberWithFloat:6.25];.