我有一个自定义视图,我想每 3 秒旋转 90 度,下面是我正在使用的代码
private func _rotateSelf(duration: Double = 1.0, delay: Double = 3.0) {
print("self._rotateSelf() called from UICustomView")
UIView.animate(withDuration: duration, delay: delay, options: [.curveEaseInOut], animations: {
// TODO: this only works on the first function call
self.transform = CGAffineTransform.identity.rotated(by: (.pi / 2))
}, completion: { finished in
if finished == true {
print("Animation finished")
self._rotateSelf()
}
})
}
问题是动画仅在第一次调用该函数时才起作用。此后的每次调用都不会发生动画,函数只会被一遍又一遍地调用,最终冻结应用程序。
我不明白什么?
您不会每次都额外旋转 90°。您仅设置 90° 总旋转。不要对
identity
变换应用旋转,而是旋转视图的当前变换。
self.transform = self.transform.rotated(by: .pi / 2)