0

我尝试使用CGAffineTransformMakeRotationand来旋转图像CGAffineTransformRotate。我想将图像从原始图像顺时针旋转到原始图像(例如 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,它将逆时针旋转(这不是我需要的)。

4

3 回答 3

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];
}
于 2013-06-13T06:38:04.943 回答
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;
}];
于 2013-06-13T06:38:36.197 回答
0

atan2()假设 A 和 B 是图像要旋转到的中心线上的两个点,则可以更好地给出旋转角度,然后旋转角度为

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

希望这可以帮助

于 2013-06-13T06:17:29.477 回答