如何创建旋转圆?

时间:2013-08-03 10:13:36

标签: iphone ios cocos2d-iphone rotation geometry

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

我是ios开发的初学者。如果有人能提供简单的示例代码,我将非常感激!

1 个答案:

答案 0 :(得分: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