2013-08-03 39 views
0

我正在尝试制作一个简单的“旋转齿轮动画”。基本上,我想在屏幕上的某个位置放一个圆圈,然后给这个圆圈一个齿轮图像(或者让图像旋转,而不是最初创建一个圆圈)。另外,齿轮应该保持旋转。我怎样才能做到这一点?如何创建一个旋转圈?

我是初学者到ios开发。如果任何人都可以提供简单的示例代码,我将非常感谢!

+0

你忘了张贴您目前拥有的代码。 – 2013-08-03 10:16:03

+0

https://github.com/jonasschnelli/UIView-i7Rotate360 – Wain

+0

谢谢!我会看看代码 – user2633210

回答

0

你必须无限地旋转UIImageView

首先#import <QuartzCore/QuartzCore.h>

- (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations: (CGFloat)rotations repeat:(float)repeat; 
{ 
    CABasicAnimation* rotationAnimation; 
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ]; 
    rotationAnimation.duration = duration; 
    rotationAnimation.cumulative = YES; 
    rotationAnimation.repeatCount = repeat; 

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 
} 

假设你有一个UIImageView并指定在其上的图像,并命名为ImgView。在viewDidLoad,添加您的代码如下:

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
[UIView setAnimationRepeatCount:MAXFLOAT]; 
ImgView.transform = CGAffineTransformMakeRotation(M_PI); 
[UIView commitAnimations]; 

从链接: -

UIView Infinite 360 degree rotation animation?

Infinite rotating image background UIImageView

+0

谢谢!我会尝试 – user2633210

+0

像Apple推荐的iOS4 +一样,使用基于块的动画不是更好吗?现在,我们几乎是iOS7 ... – holex