Question

I'm using this code to Rotate my image.

- (IBAction)spin:(id)sender {

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:5.0];
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.imgRad.transform = CGAffineTransformMakeRotation(arc4random() % 360 * M_PI / 180);
    [UIView commitAnimations];
}

Now I have two problems.

first: If the random number is greater than 90, the image moves with its center closer to the right corner. And its in relation to the random number. So higher number = the image's position is closer to the right corner. How can I fix that? Do I have to set a new center of my UIImageView every time its greater than 90?

second: If the number is for example 300, the image doesn't rotate 300 degrees, it just rotates 60 degrees the other way. How can i fix this problem?

Thanks a lot!

Was it helpful?

Solution

  • The first problem may cause by the wrong anchor point when using transform which can avoid using CAAnimation.
  • The second problem can be solved using NSNumber.

The code :

    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotation.fromValue = [NSNumber numberWithFloat:0];
    rotation.toValue = [NSNumber numberWithFloat:(arc4random() % 360 * M_PI / 180)];
    rotation.duration = 5.0f;
    rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    rotation.fillMode = kCAFillModeForwards;
    rotation.removedOnCompletion = NO;

    [self.imgRad.layer addAnimation:rotation forKey:@"rotatationRandom"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top