我有一个活动指示器,我在长时间运行的过程之前显示。 在此过程之后,我只是尝试再次删除该指标。
我无法做到。指示器显示但随后在我的屏幕上永远停留,即使我在主线程中将其删除。
有人可以帮助我吗?
代码:
// Activity Indicator Variables
var messageFrame = UIView()
var activityIndicator = UIActivityIndicatorView()
var strLabel = UILabel()
// in viewdidappear
showprogressIndicator("Please Wait", true)
dispatch_async(dispatch_get_main_queue()) {
sleep(2); // LONG RUNNING TASK
dispatch_async(dispatch_get_main_queue()) {
for subview in self.messageFrame.subviews {
subview.removeFromSuperview();
}
self.messageFrame.removeFromSuperview()
//self.saveButton.enabled = true
}
}
func showprogressIndicator(msg:String, _ indicator:Bool ) {
var strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
strLabel.text = msg
strLabel.textColor = UIColor.whiteColor()
var messageFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
messageFrame.layer.cornerRadius = 15
messageFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
if indicator {
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityIndicator.startAnimating()
messageFrame.addSubview(activityIndicator)
}
messageFrame.addSubview(strLabel)
view.addSubview(messageFrame)
//self.messageFrame.removeFromSuperview()
}
答案 0 :(得分:2)
您的代码中有多处错误。 Sleep()不是延迟执行某一部分的函数。请查看修订版本并将其作为参考。
var messageFrame: UIView!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
showprogressIndicator("Please Wait", true)
dispatch_after(dispatch_time(
DISPATCH_TIME_NOW,
Int64(2 * Double(NSEC_PER_SEC))
), dispatch_get_main_queue(), { () -> Void in
for subview in self.messageFrame.subviews {
subview.removeFromSuperview();
}
self.messageFrame.removeFromSuperview()
})
}
func showprogressIndicator(msg:String, _ indicator:Bool ) {
let strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
strLabel.text = msg
strLabel.textColor = UIColor.whiteColor()
messageFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
messageFrame.layer.cornerRadius = 15
messageFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
if indicator {
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityIndicator.startAnimating()
messageFrame.addSubview(activityIndicator)
}
messageFrame.addSubview(strLabel)
view.addSubview(messageFrame)
}
答案 1 :(得分:1)
您的代码问题是您在显示进度指示器的同时向var messageFrame
添加了另一个对象(self.view
),但是当轮到它将其删除时,您将删除一个完全不同的对象(self.messageFrame
)。
要解决此问题,请从showprogressIndicator
方法中删除本地声明,然后直接将分配的对象分配给self.messageFrame
。
self.messageFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))