我正在尝试旋转UIImageView360度,并在线查看了几个教程.我可以让他们都没有工作,没有UIView停止,或跳到一个新的位置.
我尝试过的最新事情是:
[UIView animateWithDuration:1.0
delay:0.0
options:0
animations:^{
imageToMove.transform = CGAffineTransformMakeRotation(M_PI);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
Run Code Online (Sandbox Code Playgroud)
但是如果我使用2*pi,它根本不会移动(因为它是相同的位置).如果我尝试做pi(180度),它可以工作,但如果我再次调用该方法,它会向后旋转.
编辑:
[UIView animateWithDuration:1.0
delay:0.0
options:0
animations:^{
[UIView setAnimationRepeatCount:HUGE_VALF];
[UIView setAnimationBeginsFromCurrentState:YES];
imageToMove.transform = CGAffineTransformMakeRotation(M_PI);
}
completion:^(BOOL finished){
NSLog(@"Done!");
}];
Run Code Online (Sandbox Code Playgroud)
也不起作用.它会转到180度数,暂停一瞬间,然后在重新0开始之前重新设置为度数.
我想将按钮用作切换按钮-单击一次即可无限旋转图片。再次单击,图像停止,再次单击,重新启动。
我发现此答案有助于使动画继续进行: 在Swift中将视图无限地旋转360度?
但是,我不清楚如何停止事情。我已经实现了下面的代码,而且似乎可以正常工作,但是很好奇这是否是停止动画的正确方法,或者是否还有其他首选方法。另外-我的旋转持续进行到完成为止,但是我想知道是否可以在按下按钮时冻结该位置的旋转(我在下面的第二次尝试中尝试了.removeAllAnimations(),但似乎在所有。
@IBOutlet weak var imageView: UIImageView!
var stopRotation = true
func rotateView(targetView: UIView, duration: Double = 1.0) {
if !stopRotation {
UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: {
targetView.transform = targetView.transform.rotated(by: CGFloat(Double.pi))
}) { finished in
self.rotateView(targetView: targetView, duration: duration)
}
}
}
@IBAction func spinPressed(_ sender: UIButton) {
stopRotation = !stopRotation
if !stopRotation {
rotateView(targetView: imageView)
}
}
Run Code Online (Sandbox Code Playgroud)
这确实有效。我还想知道是否有可能停止动画的自旋。设置方式是,动画在停止之前会完整旋转180度。我也尝试过在spinPressed动作中添加removeAnimation,并摆脱了rotateView内部的stopRotation检查,但这似乎不起作用–旋转会继续,并且如果再次按下spinPressed只会变得更快(请参见下文):
@IBOutlet weak var imageView: UIImageView!
var stopRotation = true
func rotateView(targetView: UIView, duration: Double = 1.0) …Run Code Online (Sandbox Code Playgroud) 我想在我的启动屏幕(LaunchScreen.storyboard)上旋转应用程序徽标。
此链接可帮助我获取旋转图像的代码。但是,我可以把这段代码放在哪里?
由于没有与启动屏幕关联的类文件,我该如何实现?