在iOS上使用animation旋转UIImageView 180度以上

我试图通过旋转超过180度animation移动米,但它不会工作。 我使用CGAffineTransform旋转仪表,它使用净结果进行旋转。 这意味着使用此function时,我不能selectCW或CCW旋转。 我怎样才能修改这个代码,使其旋转4弧度。 目前旋转被转换为净结果,使旋转最小化。

 [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1.25]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; CGAffineTransform transform = CGAffineTransformMakeRotation(4); self.meter.transform = transform; [UIView commitAnimations]; 

编辑:从答案我设法得到它通过这样做的工作

  [UIView animateWithDuration:1.5 animations:^{ CABasicAnimation *fullRotation; fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; fullRotation.fromValue = [NSNumber numberWithFloat:0]; fullRotation.toValue = [NSNumber numberWithFloat:((330*M_PI)/180)]; fullRotation.duration = 2.0f; fullRotation.repeatCount = 1; fullRotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; fullRotation.fillMode = kCAFillModeForwards; fullRotation.removedOnCompletion = NO; // add animation in your view [self.meter.layer addAnimation:fullRotation forKey:@"330"]; [self performSelector:@selector(stopRotate:) withObject:self.meter afterDelay:2.0]; }]; 

//在标题前缀中添加此项

  #define MAXFLOAT 0x1.fffffep+127f 

//添加旋转

  [UIView animateWithDuration:1.5 animations:^{ CABasicAnimation *fullRotation; fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; fullRotation.fromValue = [NSNumber numberWithFloat:0]; fullRotation.toValue = [NSNumber numberWithFloat:(2*M_PI))]; fullRotation.duration =1.0f; fullRotation.repeatCount = MAXFLOAT; // add animation in your view [yourView.layer addAnimation:fullRotation forKey:@"360"]; [self performSelector:@selector(stopRotate:) withObject:yourView afterDelay:1.0]; }]; 

并添加此代码停止旋转从视图

 -(void)stopRotate :(UIView *)yourView { [yourView.layer removeAnimationForKey:@"360"]; } 

试试这个,只是改变基本的属性,以符合您的要求:

  CATransform3D rotationTransform = CATransform3DMakeRotation(((4) * (180.0 / M_PI)), 0, 0, 1.0); CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform]; rotationAnimation.duration = 0.6f; rotationAnimation.cumulative = YES; rotationAnimation.repeatCount = 1; //Add this two lines rotationAnimation.fillMode = kCAFillModeForwards; rotationAnimation.removedOnCompletion = NO; [self.meter.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 

让我知道它是否适合你。

您需要使用以下macros将弧度转换为度数:

 #define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI)) 

并将值传递给函数:

 -(UIImage*)rotateImage:(UIImage*)img forAngle:(CGFloat)degree { UIView *rotatedViewBox = [[UIView alloc]initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)]; CGAffineTransform t = CGAffineTransformMakeRotation(degree); rotatedViewBox.transform = t; CGSize rotatedSize = rotatedViewBox.frame.size; UIGraphicsBeginImageContext(rotatedSize); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, rotatedSize.width, rotatedSize.height); CGContextRotateCTM(context, radian); CGContextScaleCTM(context, 1.0, -1.0); CGContextDrawImage(context, CGRectMake(-img.size.width, -img.size.height, img.size.width, img.size.height), img.CGImage); UIImage *returnImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return returnImg; }