views:

203

answers:

1

Hi, I am trying to make a circle image which rotates when the user drags the wheel

This is my code:

CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat:angleRadians];
rotationAnimation.duration = 10;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

The main issue I am having though, that the image is being reset when it is finished.

Any help please?

+3  A: 

Hi Lily,

I think you're approaching this problem wrong. Core Animation is primarily intended for animating transitory effects such as view transitions, fades, etc. After animating a layer, CA discards its working values and the original state of the layer is restored, which is why your image is resetting. Whilst it is no doubt possible to do what you want using CA, I think it is the hard way.

To make an image track the user's finger, I'd suggest simply calculating the angle you want from the current touch position, and transform the image for that angle whenever the touch position changes:-

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  double angle = /* calculate your angle in radians here */;
  imageView.transform = CGAffineTransformMakeRotation( angle );
}
Echelon
Thanks a million!
Lily