我正在尝试实现自定义活动指示器,它是简单的旋转 UIImageView。我有下一个代码
import Foundation
import UIKit
class CatSpinnerAnimation: UIView {
let catImageView: UIImageView = {
let catImageView = UIImageView()
catImageView.image = UIImage(named: "CatDateIcon-1024")
catImageView.translatesAutoresizingMaskIntoConstraints = false
return catImageView
}()
func startRotating(superView: UIView) {
superView.addSubview(catImageView)
UIApplication.shared.beginIgnoringInteractionEvents()
catImageView.centerXAnchor.constraint(equalTo: superView.centerXAnchor).isActive = true
catImageView.centerYAnchor.constraint(equalTo: superView.centerYAnchor).isActive = true
catImageView.widthAnchor.constraint(equalToConstant: 50).isActive = true
catImageView.heightAnchor.constraint(equalToConstant: 50).isActive = true
let rotate = CGAffineTransform(rotationAngle: CGFloat.pi)
UIView.animate(withDuration: 5.0) {
self.catImageView.transform = rotate
}
}
func stopRotating(superView: UIView) {
UIApplication.shared.endIgnoringInteractionEvents()
catImageView.removeFromSuperview()
}
}
在我的 ViewControllers 中,我实例化了这个类的一个实例并调用它的函数
let catSpinnerAnimation = CatSpinnerAnimation()
catSpinnerAnimation.startRotating(superView: view)
但这根本不显示旋转动画,只是我的 ImageView 处于最终位置(旋转了 189 度)。我尝试了多种类型的动画,但仍然没有必要的结果。我将不胜感激旋转动画的正确行为的任何解释
我认为您可能需要先将视图添加到视图层次结构中,然后才能对其进行动画处理。尝试将您的呼叫包装UIView.animate()
在呼叫中DispatchQueue.main.asyncAfter(deadline: .now() + .01) {}
这将使您的视图添加到视图层次结构并在您提交动画之前进行渲染。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句