【问题标题】:How to create a rotating circle?如何创建一个旋转的圆圈?
【发布时间】:2013-08-03 10:13:35
【问题描述】:

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

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

【问题讨论】:

标签: iphone ios cocos2d-iphone rotation geometry


【解决方案1】:

您实际上必须无限旋转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

【讨论】:

  • 使用 Apple 推荐的 iOS4+ 的基于块的动画不是更好吗?现在,我们几乎是 iOS7...
猜你喜欢
  • 2019-04-25
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 2019-07-19
相关资源
最近更新 更多