objective-c – 如何连续旋转图像

前端之家收集整理的这篇文章主要介绍了objective-c – 如何连续旋转图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个.png格式的轮子图像,我想知道我怎么能动画,以便它连续旋转,我搜索stackoverflow并找到某些代码片段,帮助我旋转我的图像,但它不会连续旋转,它会只需旋转几秒钟即可停止,代码如下

viewdidload中的代码

  1. UIImageView *imageToMove =
  2. [[UIImageView alloc] initWithImage:[UIImageimageNamed:@"horo_circle.png"]];
  3. [self.view addSubview:imageToMove];
  4.  
  5. [self rotateImage:imageToMove duration:5.0
  6. curve:UIViewAnimationCurveEaseIn degrees:180];

和动画

  1. - (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration
  2. curve:(int)curve degrees:(CGFloat)degrees
  3. {
  4. // Setup the animation
  5. [UIView beginAnimations:nil context:NULL];
  6. [UIView setAnimationDuration:duration];
  7. [UIView setAnimationCurve:curve];
  8. [UIView setAnimationBeginsFromCurrentState:YES];
  9.  
  10. // The transform matrix
  11. CGAffineTransform transform =
  12. CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
  13. image.transform = transform;
  14.  
  15. // Commit the changes
  16. [UIView commitAnimations];
  17. }

以及导入后的以下行

  1. #define M_PI 3.14159265358979323846264338327950288 /* pi */
  2.  
  3. #define DEGREES_TO_RADIANS(angle) (angle / 180.0 * M_PI)

解决方法

您最好使用CABasicAnimation执行此操作:
  1. if ([self.spinnerOverlay animationForKey:@"SpinAnimation"] == nil) {
  2. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  3. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  4. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  5. animation.duration = 10.0f;
  6. animation.repeatCount = INFINITY;
  7. [self.spinnerOverlay.layer addAnimation:animation forKey:@"SpinAnimation"];
  8. }

在这段代码中,我检查动画是否全部准备就绪,不需要再次设置.
在您的情况下,spinnerOverlay是您要旋转的UIImageView.

要停止动画:

  1. [self.spinnerOverlay.layer removeAnimationForKey:@"SpinAnimation"];

猜你在找的C&C++相关文章