我实际上是为UIView创建了一个扩展。所以我只使用ViewController的第一个视图来执行此操作:
view.displayBusy() // you don't need 'self.view' in Swift
...
view.displayNotBusy()
我也试过这个:
view.displayBusy()
DispatchQueue.global().async {
doSomething()
DispatchQueue.main.async {
doSomethingComplete()
}
}
func doSomethingComplete() {
view.displayNotBusy()
}
也不工作。
这是我的扩展。我添加了子视图,我把它带到了前面。但我看不到它。我还设置断点并在更新约束后检出框架。它位于市中心。我已经测试了没有'displayNotBusy',并且'hidesWhenStopped'设置为false只是为了确保它没有通过忙碌区域直接跳转。我做了128号。
我看不到它。有谁知道为什么我没有看到它?
extension UIView {
func displayBusy() {
var busyView : UIActivityIndicatorView? = viewWithTag(0x4255) as? UIActivityIndicatorView
if busyView == nil {
busyView = UIActivityIndicatorView(frame: CGRect(x: 50, y: 50, width: 32, height: 32))
busyView!.tag = 0x4255
addSubview(busyView!)
busyView!.translatesAutoresizingMaskIntoConstraints = false
let widthConstraint = NSLayoutConstraint(item: busyView!, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 32)
let heightConstraint = NSLayoutConstraint(item: busyView!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 32)
addConstraints([widthConstraint, heightConstraint])
let horConstraint = NSLayoutConstraint(item: busyView!, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
let verConstraint = NSLayoutConstraint(item: busyView!, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)
addConstraints([horConstraint, verConstraint])
// Now make it all happen
setNeedsUpdateConstraints()
setNeedsLayout()
layoutIfNeeded()
}
self.bringSubview(toFront: busyView!)
busyView!.hidesWhenStopped = true
busyView!.isHidden = false
busyView!.startAnimating()
}
func displayNotBusy() {
let busyView : UIActivityIndicatorView? = viewWithTag(0x4255) as? UIActivityIndicatorView
if busyView != nil {
busyView!.stopAnimating()
}
}
}