Ios 连续旋转不居中
使用此代码:Ios 连续旋转不居中,ios,swift,autolayout,Ios,Swift,Autolayout,使用此代码: UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat], animations: { self.star.transform = self.star.transform.rotated(by: CGFloat(M_PI_2)) }) extension UIView { func rotate360Degrees(duration: CFTimeInterval = 3)
UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat], animations: {
self.star.transform = self.star.transform.rotated(by: CGFloat(M_PI_2))
})
extension UIView {
func rotate360Degrees(duration: CFTimeInterval = 3) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(M_PI * 2)
rotateAnimation.isRemovedOnCompletion = false
rotateAnimation.duration = duration
rotateAnimation.repeatCount=Float.infinity
self.layer.add(rotateAnimation, forKey: nil)
}
}
我的观点是:
使用此代码:
UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat], animations: {
self.star.transform = self.star.transform.rotated(by: CGFloat(M_PI_2))
})
extension UIView {
func rotate360Degrees(duration: CFTimeInterval = 3) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(M_PI * 2)
rotateAnimation.isRemovedOnCompletion = false
rotateAnimation.duration = duration
rotateAnimation.repeatCount=Float.infinity
self.layer.add(rotateAnimation, forKey: nil)
}
}
我的观点是:
两人都不是我想要的。正在旋转的视图是要填充比例的UIImageView。我希望图像保持在中间。我怎样才能做到这一点?这些函数在ViewDidDisplay中执行。最后一张gif看起来好多了,但请注意星星并没有完全居中 问题
你图像的中心不是你的星星的中心
解决
有两种可能的解决办法
- 编辑图像,使星星的中心位于图像的中心
- 将旋转层的锚定点设置到恒星的中心(x:1004px,y:761px)
- 编辑图像,使星星的中心位于图像的中心
- 将旋转层的锚定点设置到恒星的中心(x:1004px,y:761px)