2013-06-13 56 views
0

我尝试使用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将逆时针旋转(这不正是我需要的)。我不知道我做错了什么,谁能告诉我?

回答

0

旋转角度由atan2()假设A和B给出更好的是在中心线两点,你的图像将被旋转然后旋转角度为

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

希望这有助于

0
Try this : 

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

使用:

[UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.1]; 
    [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; 
    }];