xcode 如何快速创建旋转轮/图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36319438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:41:05  来源:igfitidea点击:

How do I create a rotating wheel/image in swift?

xcodeswift

提问by Joaquin van Loon

I'm a beginner app developer. I want to make an app with a spinning/rotating wheel in it, like the one in trivia crack. My idea was to use an UIImage with holds an image of a wheel. When the user taps the image, I want to let the image spin. This isn't difficult for me, but the difficult part for me is to let the wheel spin to a certain category on the wheel. Do you guys have any idea how I should do this. I thought that I could work with degrees and angles, but I don't know how to work with degrees in swift. I hope that some of you can help me!

我是初学者应用程序开发人员。我想制作一个带有旋转/旋转轮的应用程序,就像琐事破解中的应用程序一样。我的想法是使用带有轮子图像的 UIImage 。当用户点击图像时,我想让图像旋转。这对我来说并不难,但对我来说困难的部分是让轮子旋转到轮子上的某个类别。你们知道我该怎么做吗?我以为我可以使用度数和角度,但我不知道如何使用swift. 我希望你们中的一些人可以帮助我!

采纳答案by Rachel

You can use NSTimer to spin the wheel. when you tap the image, it'll trigger timer to start rotate it,

您可以使用 NSTimer 来旋转轮子。当您点击图像时,它会触发计时器开始旋转它,

timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "rotateWheel", userInfo: nil, repeats: true)

func rotateWheel() {
    //set your angle here
    self.wheel.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
}

and when you want to stop rotating wheel. You can just call below,

当你想停止旋转轮子时。你可以在下面打电话,

timer.invalidate()

回答by Oleg Gordiichuk

If you would like to rotate image of the wheel. Use sample code that use transformproperty of the image and rotate it for 180 degrees.

如果您想旋转轮子的图像。使用使用transform图像属性的示例代码并将其旋转 180 度。

 UIView.animateWithDuration(2.0, animations: {
      self.wheel.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
 })

回答by Ryan

Once I've done very similar job before. Even it was not in swift, I believe it will not be difficult for you to try.

一旦我以前做过非常相似的工作。即使没有swift,我相信你尝试一下也不难。

CABasicAnimation *animation = (CABasicAnimation *)[view.layer animationForKey:@"rotationAnimation"];
NSNumber *toValue = [NSNumber numberWithFloat: (M_PI * 2 * round) + [animation.toValue floatValue]];
CGFloat duration = round / speed;

CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.fromValue     = animation.toValue;
rotationAnimation.toValue       = toValue;
rotationAnimation.duration      = duration;
rotationAnimation.cumulative    = YES;
rotationAnimation.autoreverses  = NO;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.fillMode      = kCAFillModeForwards;
rotationAnimation.additive      = YES;
rotationAnimation.repeatCount   = 1;

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

You can set toValueto stop rotation as you want.

您可以根据toValue需要设置停止旋转。