Ios 在视图控制器之间切换时,图像旋转动画会加速吗?
我有一个应用程序,其中有一个Ios 在视图控制器之间切换时,图像旋转动画会加速吗?,ios,swift,uiimageview,uiviewanimation,Ios,Swift,Uiimageview,Uiviewanimation,我有一个应用程序,其中有一个UIImageView,正在使用UIView顺时针旋转。动画制作如下: func startAnimation(targetView: UIView, duration: Double = 6) { UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: { targetView.transform = targetView.tr
UIImageView
,正在使用UIView顺时针旋转。动画制作如下:
func startAnimation(targetView: UIView, duration: Double = 6)
{
UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: {
targetView.transform = targetView.transform.rotated(by: CGFloat(Double.pi))
})
{ finished in
self.startAnimation(targetView: targetView, duration: duration)
}
}
此函数仅在viewdiapear()
中调用(主要原因是因为我还有两个UIViewControllers
,用户可以来回使用
我还有一个stopAnimation()
函数,在viewwilldemouse()
中调用。主要目标是在用户看到另一个视图控制器时停止动画
func stopAnimation()
{
self.view.layer.removeAllAnimations()
self.view.layoutIfNeeded()
}
目标:当用户停留在同一个视图控制器上时,动画应该是无限的,但当用户切换到另一个视图控制器时,动画应该停止,当用户返回到该视图控制器时,动画应该以相同的恒定速度重新开始
问题:由于某种原因,每次在另一个视图控制器和具有该动画的视图控制器之间切换时,动画都会加速。我不确定是什么原因导致了这种情况
编辑:根据要求添加代码:
override func viewDidAppear(_ animated: Bool)
{
super.viewDidAppear(animated)
startAnimation(targetView: logo)
}
每次开始设置动画之前,您都需要重置变换。否则,它可能已经接近旋转结束的一半。因此需要更短的时间或更远的距离=更长的时间。也可以使用。重复而不是递归
func startAnimation(targetView: UIView, duration: Double = 6)
{
targetView.transform = targetView.transform.rotated(by: CGFloat(0.0))
UIView.animate(withDuration: duration, delay: 0.0, options: [.curveLinear, .repeat], animations: {
targetView.transform = targetView.transform.rotated(by: CGFloat(Double.pi))
})
}
你能提供你称之为startAnimation的代码吗?我已将其添加到我的帖子中。请先看一下更新。我建议你使用选项repeat运行动画,这样你就不需要在动画完成后重新启动。像这样传递选项:选项:[.curveLinear,.repeat].我还没有测试过这是否能解决你的问题,所以你也许可以测试一下。我在发帖之前已经测试过了。问题是一旦动画循环完成,它会以大约两个像素左右的速度向左移动。我不明白它为什么会移动。如果可以,那么我不会真的移动它一完成就重新开始。