Xcode 如何在swift中创建旋转的轮子/图像?
我是一个应用程序开发新手。我想做一个应用程序,里面有一个旋转的轮子,就像《琐事裂纹》中的那个。我的想法是将UIImage与车轮的图像结合使用。当用户点击图像时,我想让图像旋转。这对我来说并不困难,但对我来说困难的部分是让轮子旋转到轮子上的某个类别。你们知道我该怎么做吗。我以为我可以处理度和角度,但我不知道如何在Xcode 如何在swift中创建旋转的轮子/图像?,xcode,swift,Xcode,Swift,我是一个应用程序开发新手。我想做一个应用程序,里面有一个旋转的轮子,就像《琐事裂纹》中的那个。我的想法是将UIImage与车轮的图像结合使用。当用户点击图像时,我想让图像旋转。这对我来说并不困难,但对我来说困难的部分是让轮子旋转到轮子上的某个类别。你们知道我该怎么做吗。我以为我可以处理度和角度,但我不知道如何在swift中处理度。 我希望你们中的一些人能帮助我 如果要旋转轮子的图像。使用使用图像的变换属性的示例代码,并将其旋转180度 UIView.animateWithDuration(2.
swift
中处理度。
我希望你们中的一些人能帮助我 如果要旋转轮子的图像。使用使用图像的
变换
属性的示例代码,并将其旋转180度
UIView.animateWithDuration(2.0, animations: {
self.wheel.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
})
您可以使用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)
}
当你想停止转动轮子的时候。你可以在下面打电话
timer.invalidate()
我曾经做过类似的工作。即使它不是在
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"];
您可以将
设置为value
以根据需要停止旋转 你是什么意思旋转到某个类别
?你想旋转特定于图像的半径吗?轮子分为不同的类别(用于测验),如体育、历史和科学。所以我想让轮子旋转到其中一个类别(随机),我想看看你是如何处理所有代码的。谢谢,这对我帮助很大。警告:这将设置单个旋转的动画,然后将停止。