iPhone - UIImageView在旋转动画时移动> 90度

时间:2014-04-21 12:20:03

标签: ios objective-c animation uiimageview

我使用此代码来旋转图像。

- (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];
}

现在我有两个问题。

first:如果随机数大于90,则图像会以其中心靠近右角移动。它与随机数有关。如此高的数字=图像的位置更接近右角。我该如何解决这个问题?每次超过90时,我是否必须设置UIImageView的新中心?

秒:如果数字为例如300,则图像不会旋转300度,它只是以另一种方式旋转60度。我该如何解决这个问题?

非常感谢!

1 个答案:

答案 0 :(得分:2)

  • 使用变换时,第一个问题可能是由错误的锚点引起的,这可能会导致使用CAAnimation
  • 第二个问题可以使用NSNumber解决。

代码:

    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"];
相关问题