CGAffineTransformMakeRotation逆时针总是

我试图animation的UIImageView逆时针旋转,当用户触摸它,并在顺时针方向上移动它的手指。
基本上我想UIImageView回到原来的位置,一旦触摸结束,它应该逆时针方向移动。

我遇到的问题是,每次angular度(以度数计)大于180时,图像将顺时针旋转回原来的位置。 它显然走了最短的路。 至于angular度179度或更less的图像逆时针旋转(我需要什么)。

我试了这两个代码段,他们的行为是一样的。 有什么build议么?

注意:这里的angular度variables是一个双精度值,并且初始化为零,并且在我的代码中保持为0

 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self handleObject:touches withEvent:event isLast:YES]; /* this doesn't make rotation counter clockwise for rotations greater than 180 */ [UIView beginAnimations:nil context:NULL]; [UIView setAnimationCurve: UIViewAnimationCurveLinear]; [UIView setAnimationDuration:1.0]; [UIView setAnimationRepeatCount:1]; CGAffineTransform transform = CGAffineTransformMakeRotation(angle); circle1ImgVw.transform = CGAffineTransformRotate(transform, angle); // ends animation [UIView commitAnimations]; /* this doesn't make rotation counter clockwise for rotations greater than 180 */ [UIView animateWithDuration:1.0 animations:^{ circle1ImgVw.transform = CGAffineTransformMakeRotation(angle); }]; } 

我试着看这个post ,有几个问题

  • animation完成后,我无法触摸并旋转图像
  • 它总是以顺时针方向进行animation,而且我无法弄清楚如何从用户结束旋转图像的位置开始平滑地逆时针旋转。

我有一个类似的问题,检查接受的答案,它可能会帮助你:

顺时针旋转UIView大于180度的angular度


回应评论,也许这将有助于:

在我的代码中,我实际上使用

 rotationAnimation.fromValue rotationAnimation.toValue 

代替

 rotationAnimation.byValue. 

如果toValue小于fromValue,则旋转总是逆时针旋转。 无论你的价值观是积极的还是消极的,只是他们之间的关系而已。

找出你的开始angular度是什么,找出你要旋转的方向,找出你的结束angular度。 如果你想逆时针转动,确保它小于你的起始angular度。 以下是我用于从12:00到当前时间顺时针方向制作时钟的代码。

 -(void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self updateClockArmAngle]; } - (void)updateClockArmAngle { // Get the time NSDate *date = [NSDate date]; NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]]; NSDateComponents* components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date]; CGFloat hour = [components hour]; CGFloat angle = (hour/24.0)*(2*M_PI); CGFloat minute = [components minute]; angle += (minute/(24*60))*(2*M_PI); [self rotateViewAnimated:self.clockArm withDuration:1.5 byAngle:angle]; } - (void) rotateViewAnimated:(UIView*)view withDuration:(CFTimeInterval)duration byAngle:(CGFloat)angle { CABasicAnimation *rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.fromValue = 0; rotationAnimation.toValue = [NSNumber numberWithFloat:angle]; rotationAnimation.duration = duration; rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; [rotationAnimation setRemovedOnCompletion:NO]; [rotationAnimation setFillMode:kCAFillModeForwards]; [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; } 

我使用的技术是将旋转angular度除以2,并将旋转分隔为两个单独的animation,由animateWithDuration:delay:options:animations:completion:completion:参数链接。 但是,由于视图已经旋转到当前位置,所以您需要从该位置开始,并且“反转”回零,而试图从零旋转CCW。