0

I try to make my view rotate as long as it goes through the path (linear path) but it doesn't rotate.

This is my code:

 CAKeyframeAnimation *animKF = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animKF.values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(100, 100)], [NSValue valueWithCGPoint:CGPointMake(400, 400)], [NSValue valueWithCGPoint:CGPointMake(700, 100)], [NSValue valueWithCGPoint:CGPointMake(1000, 400)], nil];

animKF.keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.25], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:1.0], nil];

animKF.duration = 6;

animKF.rotationMode =  kCAAnimationRotateAuto;

[imageView.layer addAnimation:animKF forKey:@"keyframe"];

But the imageView (the blue face in the picture) doesn't rotate along the path (the red line in the picture)

Path

Thank you so much.

torhector2
  • 443
  • 1
  • 5
  • 19

1 Answers1

2

From the documentation of rotationMode:

The effect of setting this property to a non-nil value when no path object is supplied is undefined.

In your code you are setting the values-property of your key frame animation but you need to set a path instead.


You should create a CGPath from your values and set that as the path of your keyframe.

UIBezierPath *animationPath = [UIBezierPath bezierPath];
[animationPath moveToPoint:CGPointMake(100, 100)];
[animationPath addLineToPoint:CGPointMake(400, 400)];
[animationPath addLineToPoint:CGPointMake(700, 100)];
[animationPath addLineToPoint:CGPointMake(1000, 400)];

animKF.path = animationPath.CGPath;
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205