如何将图像从原始图像旋转回来?

时间:2013-06-13 06:06:56

标签: ios cgaffinetransform

我尝试使用CGAffineTransformMakeRotationCGAffineTransformRotate来旋转图片。我想将图像从原始图像和顺时针图像再次旋转到原始图像(如0到360度)。我需要像播放器上的CD一样旋转图像。我做了这样的事情:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];  
CGAffineTransform transform = CGAffineTransformMakeRotation(1.0);
transform = CGAffineTransformRotate(transform, -180);
ImageView.transform = transform;

[UIView commitAnimations];

它只旋转180度,如果我将其改为-300,它将逆时针旋转(这不是我需要的)。

3 个答案:

答案 0 :(得分:0)

atan2()假设旋转角度更好,假设A和B是图像旋转到的中心线上的两个点,那么旋转角度为

0-atan2((b.x - a.x) ,(b.y -a.y))

希望这有帮助

答案 1 :(得分:0)

试试这个:

-(void)startAnimationWithRevolutions:(float)revPerSecond forTime:(float)time
{
    spinWheel.userInteractionEnabled = FALSE;
    float totalRevolutions = revPerSecond * time;
    [CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:time] forKey:kCATransactionAnimationDuration];

    CABasicAnimation* spinAnimation = [CABasicAnimation
                                       animationWithKeyPath:@"transform.rotation"];
    CGAffineTransform transform = spinWheel.transform;
    float fromAngle = atan2(transform.b, transform.a);
    float toAngle = fromAngle + (totalRevolutions*4*M_PI);
    spinAnimation.fromValue = [NSNumber numberWithFloat:fromAngle];
    spinAnimation.toValue = [NSNumber numberWithFloat:toAngle];
    spinAnimation.repeatCount = 0;
    spinAnimation.removedOnCompletion = NO;
    spinAnimation.delegate = self;
    spinAnimation.timingFunction = [CAMediaTimingFunction functionWithName:
                                    kCAMediaTimingFunctionEaseOut];
    [spinWheel.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
    [CATransaction commit];
}

答案 2 :(得分:0)

使用此:

[UIView animateWithDuration:0.1 animations:^{
    CGAffineTransform transform = CGAffineTransformMakeRotation(1.0);
    transform = CGAffineTransformRotate(transform, -180);
    ImageView.transform = transform;
} completion:^(BOOL finished) {
    CGAffineTransform transform = CGAffineTransformMakeRotation(0);
    transform = CGAffineTransformRotate(transform,0);
    ImageView.transform = transform;
}];