【发布时间】:2018-01-21 11:30:43
【问题描述】:
我有以下类来添加和删除活动指示器视图:
import Foundation
import UIKit
class ViewControllerUtils {
var container: UIView = UIView()
var loadingView: UIView = UIView()
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
func showActivityIndicator(uiView: UIView) {
container.frame = uiView.frame
container.center = uiView.center
container.backgroundColor = UIColorFromHex(rgbValue: 0xffffff, alpha: 0.3)
loadingView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
loadingView.center = uiView.center
loadingView.backgroundColor = UIColorFromHex(rgbValue: 0x444444, alpha: 0.7)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 10
activityIndicator.frame = CGRect(x: 0, y: 0, width: 40, height: 40);
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
activityIndicator.center = CGPoint(x: loadingView.frame.size.width / 2, y: loadingView.frame.size.height / 2);
loadingView.addSubview(activityIndicator)
container.addSubview(loadingView)
uiView.addSubview(container)
activityIndicator.startAnimating()
}
func hideActivityIndicator(uiView: UIView) {
print("hideActivityIndicator called")
activityIndicator.stopAnimating()
activityIndicator.removeFromSuperview()
loadingView.removeFromSuperview()
container.removeFromSuperview()
}
func UIColorFromHex(rgbValue:UInt32, alpha:Double=1.0)->UIColor {
let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
let blue = CGFloat(rgbValue & 0xFF)/256.0
return UIColor(red:red, green:green, blue:blue, alpha:CGFloat(alpha))
}
}
我在 tableview 和 view 中调用函数,方法如下:
override func viewDidLoad() {
super.viewDidLoad()
ViewControllerUtils().showActivityIndicator(uiView: self.tableView)
invitedEvents()
}
加载数据后,我尝试删除活动指示器,但没有删除。
func invitedEvents() {
//calling firebase
DataService.ds.REF_USER_CURRENT.observe(.value, with: { (snap) in
ViewControllerUtils().hideActivityIndicator(uiView: self.tableView)
})
}
当 tableview 正在加载时,正在打印 hideActivityIndicator 被调用,但它没有从视图中删除。
我也在视图中调用函数,结果相同:showActivityIndicator 工作,hideActivityIndicator 不工作。
我使用以下参数在视图中调用函数:
ViewControllerUtils().showActivityIndicator(uiView: self.view)
ViewControllerUtils().hideActivityIndicator(uiView: self.view)
有人知道我错过了什么吗?
【问题讨论】:
标签: ios uitableview swift3 uiview