RSM
RSM

Reputation: 330

How to animate an image in backward using CAKeyFrameAnimation

I'm new in iOS development. I'm creating a Keyframe animation for rotating the image.The image was successfully rotated in forward.I want to rotate the image in full and full backward.

This is my code.

UIImageView *tempView = [[UIImageView alloc] initWithFrame:CGRectMake(28,158,133,133)];
tempView.image = [UIImage imageNamed:@"1_03.png"];
[self.view addSubview:tempView];

const NSUInteger rotations = 1;
const NSTimeInterval duration  = 4.0f;

CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
CGFloat touchUpStartAngle = 0;
CGFloat touchUpEndAngle = (M_PI);
CGFloat angularVelocity = (((2 * M_PI) * rotations) + M_PI) / duration;
anim.values = @[@(touchUpStartAngle), @(touchUpStartAngle + angularVelocity * duration)];
anim.duration = duration;
anim.delegate = self;
anim.repeatCount = INFINITY;
[tempView.layer addAnimation:anim forKey:@"animation"];

tempView.transform = CGAffineTransformMakeRotation(touchUpStartAngle +     (touchUpEndAngle));

How to do this.

Upvotes: 1

Views: 218

Answers (2)

Ravindhiran
Ravindhiran

Reputation: 5384

Try this

CGFloat touchUpStartAngle = 0;
CGFloat touchUpEndAngle = (M_PI);
CGFloat angularVelocity = (((2 * M_PI) * rotations) + M_PI) / duration;
anim.values = @[@(touchUpStartAngle), @(touchUpStartAngle - angularVelocity * duration)];

Upvotes: 1

SBM
SBM

Reputation: 1043

Try this.

UIImageView *tempView = [[UIImageView alloc] initWithFrame:CGRectMake(28,158,133,133)];
tempView.image = [UIImage imageNamed:@"1_03.png"];
[self.view addSubview:tempView];

const NSUInteger rotations = 1;
const NSTimeInterval duration  = 4.0f;

CAKeyframeAnimation *anim = [CAKeyframeAnimation      animationWithKeyPath:@"transform.rotation"];
CGFloat touchUpStartAngle = 0;
CGFloat touchUpEndAngle = (M_PI);
CGFloat angularVelocity = (((2 * M_PI) * rotations) + M_PI) / duration;
anim.values = @[@(touchUpStartAngle), @(touchUpStartAngle - angularVelocity * duration)];
anim.duration = duration;
anim.delegate = self;
anim.repeatCount = INFINITY;
[tempView.layer addAnimation:anim forKey:@"animation"];

tempView.transform = CGAffineTransformMakeRotation(touchUpStartAngle +     (touchUpEndAngle));

Upvotes: 3

Related Questions