我在UIViewController中有一个UITableView。UIViewController有三个实例变量:
@IBOutlet weak var tableView: UITableView!
var loadingIndicatorFooterView: UIView!
var searchAgainPromptFooterView: UIView!
此表用于显示搜索结果。有时我想显示
loadingIndicatorFooterView
(分页结果),有时我想显示searchAgainPromptFooterView
(不再显示结果)。My
loadingIndicatorFooterView
是车轮的旋转图像,旋转通过以下方式实现:class SpinnerView : UIImageView {
convenience init() {
self.init(image: UIImage(named: "Spinner"))
rotateInfinitely()
}
func rotateInfinitely() {
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotationAnimation.fromValue = 0.0
rotationAnimation.toValue = Double.pi * 2 //Minus can be Direction
rotationAnimation.duration = 1
rotationAnimation.repeatCount = .infinity
self.layer.add(rotationAnimation, forKey: nil)
}
}
我启动并将
loadingIndicatorFooterView
和searchAgainPromptFooterView
存储在视图控制器的viewDidLoad
函数中。如果我将tableView.tableFooterView
设置为loadingIndicatorFooterView
中的viewDidLoad
,我确实看到车轮打滑。但我现在没有设置tableView.tableFooterView
,所以它是nil
。稍后,当我执行搜索时,在搜索功能中有一行:
tableView.tableFooterView = loadingIndicatorFooterView
我确实看到了方向盘,但它不转。
这是我实例化
SpinnerView
并存储到loadingIndicatorFooterView
中的代码:loadingIndicatorFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 50))
let loadingIndicator = SpinnerView()
loadingIndicator.translatesAutoresizingMaskIntoConstraints = false
loadingIndicatorFooterView.addSubview(loadingIndicator)
// < add constraints >
最佳答案
类微调器可以是下一个:
class Spinner: UIViewController {
var spinner = UIActivityIndicatorView(style: .whiteLarge)
override func loadView() {
view = UIView()
view.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
spinner.translatesAutoresizingMaskIntoConstraints = false
spinner.startAnimating()
view.addSubview(spinner)
spinner.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
spinner.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
您应该创建微调器,请参阅此计时器DispatchQueue.main.asyncAfter(截止日期:.now()+12){
func createSpinnerView() {
let child = Spinner()
self.view.addSubview(child.view)
child.view.frame = CGRect(x: 100, y: 400, width: 100, height: 100)
child.didMove(toParent: self)
DispatchQueue.main.asyncAfter(deadline: .now() + 12) {
child.willMove(toParent: nil)
child.view.removeFromSuperview()
child.removeFromParent()
}
}
你的观点
override func viewDidLoad() {
super.viewDidLoad()
self.createSpinnerView()
}