我有以下代码,我无法弄清楚为什么我的活动指示器没有显示。 我不确定为什么它不会显示。我基本上具有围绕连接到API并返回JSON数据的代码的启动和停止动画功能。根据连接速度,事务将随时间变化。我希望此活动指示器在发生这种情况时显示。它与DispatchQueue有关吗
self.activityIndicatorView = ActivityIndicatorView(title: "Converting...", center: self.view.center)
self.view.addSubview(self.activityIndicatorView.getViewActivityIndicator())
self.activityIndicatorView.startSpinner()
UIApplication.shared.beginIgnoringInteractionEvents()
//Run the fetchConversion regardless
//Codes is run here to get a JSON response removed to clarity but this can take some time depending on internet connection speeds.
UIApplication.shared.endIgnoringInteractionEvents()
self.activityIndicatorView.stopSpinner()
在活动指示器视图中调用此功能
class ActivityIndicatorView
{
var view: UIView!
var activityIndicator: UIActivityIndicatorView!
var title: String!
init(title: String, center: CGPoint, width: CGFloat = 200.0, height: CGFloat = 50.0)
{
self.title = title
let x = center.x - width/2.0
let y = center.y - height/2.0
self.view = UIView(frame: CGRect(x: x, y: y, width: width, height: height))
self.view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) // UIColor(red: 255.0/255.0, green: 204.0/255.0, blue: 51.0/255.0, alpha: 0.5)
self.view.layer.cornerRadius = 10
self.activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.activityIndicator.color = UIColor.black
self.activityIndicator.hidesWhenStopped = false
let titleLabel = UILabel(frame: CGRect(x: 60, y: 0, width: 200, height: 50))
titleLabel.text = title
titleLabel.textColor = UIColor.black
self.view.addSubview(self.activityIndicator)
self.view.addSubview(titleLabel)
}
func getViewActivityIndicator() -> UIView
{
return self.view
}
func startSpinner()
{
self.activityIndicator.startAnimating()
UIApplication.shared.beginIgnoringInteractionEvents()
}
func stopSpinner()
{
self.activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
self.view.removeFromSuperview()
}
}
非常感谢任何帮助