![](/img/trans.png)
[英]Swift: starting and stopping the animation of an activity indicator inside a custom class
[英]Activity indicator animation not working - Swift
我在加載屏幕時遇到了這個問題,我啟動了一個活動指示器並調用了一個函數來獲取數據。 起初,活動顯示但從未啟動,然后我在不同的線程中運行每個調用(啟動活動和獲取數據)。 動畫工作但只有一次,當我嘗試再次加載屏幕時,它再次卡住,直到加載數據。 這是 viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
//indicator
indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
indicator.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
indicator.color = commons.getButtonColor()
indicator.center = self.view.center
indicator.hidesWhenStopped = true
self.view.addSubview(indicator)
DispatchQueue.main.async {
print("We finished that.")
// only back on the main thread, may you access UI:
self.indicator.startAnimating()
}
//connect to database
commons.ref = Database.database().reference()
//table view
tableV.dataSource = self
tableV.delegate = self
tableV.tableFooterView = UIView()
tableV.separatorStyle = .none
tableV.allowsSelection = false
//change left btn
let leftButtonItem = UIBarButtonItem.init(
title: "",
style: .done,
target: self,
action: #selector(backButtonPressed)
)
leftButtonItem.image = UIImage(named:"back")
leftButtonItem.tintColor = commons.getButtonColor()
self.navigationItem.leftBarButtonItem = leftButtonItem
//style
self.title = flag
DispatchQueue.main.async {
//get values
if(self.flag == "Accounts"){
self.color = "d0f0dd"
self.getAccounts()
}else if(self.flag == "Loans"){
self.color = "f8ff90"
self.getLoans()
}else if(self.flag == "Cards"){
self.color = "ffa669"
self.getCards()
}
}
}
屏幕卡住時的截圖:
編輯:獲取數據的函數:
func getAccounts(){
commons.ref.child("Accounts").observeSingleEvent(of: .value, with: { (snapshot) in
for item in snapshot.children {
self.values.append(item as! DataSnapshot)
}
self.tableV.reloadData()
self.indicator.stopAnimating()
}) { (error) in
print(error.localizedDescription)
}
}
func getCards(){
commons.ref.child("Cards").observeSingleEvent(of: .value, with: { (snapshot) in
self.indicator.stopAnimating()
for item in snapshot.children {
self.values.append(item as! DataSnapshot)
}
self.tableV.reloadData()
}) { (error) in
print(error.localizedDescription)
}
}
func getLoans(){
commons.ref.child("Loans").observeSingleEvent(of: .value, with: { (snapshot) in
self.indicator.stopAnimating()
for item in snapshot.children {
self.values.append(item as! DataSnapshot)
}
self.tableV.reloadData()
}) { (error) in
print(error.localizedDescription)
}
}
您的所有 3 個函數都存在一個常見錯誤。 您不會在代碼的所有可能分支上調用stopAnimating
,因此如果調用完成處理程序時error
,您的活動指示器將繼續運行。
我不確定 Firebase 的完成處理程序是如何實現的,它們可能已經被分派到主隊列,但為了絕對安全,我還建議將stopAnimating
函數調用分派到主線程以確保您不會意外嘗試從后台線程更新 UI。
func getAccounts(){
commons.ref.child("Accounts").observeSingleEvent(of: .value, with: { (snapshot) in
for item in snapshot.children {
self.values.append(item as! DataSnapshot)
}
DispatchQueue.main.async{
self.tableV.reloadData()
self.indicator.stopAnimating()
}
}) { (error) in
print(error.localizedDescription)
DispatchQueue.main.async{
self.indicator.stopAnimating()
}
}
}
您應該對其他兩個函數進行相同的更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.