2015-04-28 69 views
20

當我在標籤頁之間切換時,它加載了幾秒鐘,我想知道我的數據正在加載。爲此,我決定添加一個活動指標。如何在tableView加載時顯示活動指標?

我寫了一個小功能:

func showActivityIndicator() { 
    dispatch_async(dispatch_get_main_queue()) { 
     self.spinner = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge) 
     self.spinner.frame = CGRect(x: 0.0, y: 0.0, width: 80.0, height: 80.0) 
     self.spinner.center = CGPoint(x:self.loadingView.bounds.size.width/2, y:self.loadingView.bounds.size.height/2) 

     self.loadingView.addSubview(self.spinner) 
     self.view.addSubview(self.loadingView) 
     self.spinner.startAnimating() 
    } 
} 

,會告訴我的指示。當我從我的infoController點擊按鈕時嘗試使用它:

@IBAction func goToMainFromInfo(sender: AnyObject) { 
     self.showActivityIndicator() 
     self.performSegueWithIdentifier("fromMainToInfoWActivity", sender: nil) 
     self.hideActivityIndicator() 
    } 
} 

我在segue之前顯示執行並隱藏之後。它不幫助我。當我嘗試使用同步:

@IBAction func goToMainFromInfo(sender: AnyObject) { 
    dispatch_async(dispatch_get_main_queue()) { 
     self.showActivityIndicator() 
     self.performSegueWithIdentifier("fromMainToInfoWActivity", sender: nil) 
     self.hideActivityIndicator() 
    } 
} 

但它也沒有幫助。當我按下Tab鍵時,不透明度變爲0.5,我在加載時等待。但我沒有看到我的活動指標。

什麼問題?

回答

41

就試試這個:

var indicator = UIActivityIndicatorView() 

func activityIndicator() { 
    indicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 40, 40)) 
    indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray 
    indicator.center = self.view.center 
    self.view.addSubview(indicator)  
} 

而且要啓動動畫

indicator.startAnimating() 
indicator.backgroundColor = UIColor.whiteColor() 

對於停止:

indicator.stopAnimating() 
indicator.hidesWhenStopped = true 

注:避免啓動的呼叫,並停止在同一時間。只要提供一些條件。

+0

這是明顯的微調。只是沒有在所有 –

+0

出現爲什麼你給節目胡亞蓉指標和隱藏在同一時間 – Lydia

+0

我有註釋此行'//self.hideActivityIndi​​cator()'。它仍然是一樣的。不工作 –

2

此代碼可以幫助你:)

let indicator:UIActivityIndicatorView = UIActivityIndicatorView (activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray) 
    indicator.color = UIColor .magentaColor() 
    indicator.frame = CGRectMake(0.0, 0.0, 10.0, 10.0) 
    indicator.center = self.view.center 
    self.view.addSubview(indicator) 
    indicator.bringSubviewToFront(self.view) 
    indicator.startAnimating() 
2

SWIFT

將下面這個類:

let indicator:UIActivityIndicatorView = UIActivityIndicatorView (activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray) 

將此放在您的loadView():

indicator.color = UIColor .magentaColor() 
indicator.frame = CGRectMake(0.0, 0.0, 10.0, 10.0) 
indicator.center = self.view.center 
self.view.addSubview(indicator) 
indicator.bringSubviewToFront(self.view) 
indicator.startAnimating() 

就我而言,我通過FUNC發送請求,請求JSON對象,所以我把這個在這個函數結束時刪除數據加載一次活動指示燈:

self.indicator.stopAnimating() 
self.indicator.hidesWhenStopped = true 
10

斯威夫特2+

class ViewController: UITableViewController { 
     weak var activityIndicatorView: UIActivityIndicatorView! 

     override func viewDidLoad() { 
      super.viewDidLoad() 
      let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray) 
      tableView.backgroundView = activityIndicatorView 
      self.activityIndicatorView = activityIndicatorView 
      activityIndicatorView.startAnimating() 

     } 
     ... 
    } 
+0

工作對我來說,問題是,我把它添加到視圖,這是我的tableview細胞隱藏,使用相同的背景顏色與tableview中的背景色。只要沒有單元格覆蓋視圖(這樣在加載時沒有問題),此解決方案就可以工作,除非(如我一樣)顯示一些文本的佔位符單元格。在這種情況下,將加載指示器添加到該單元可能會更容易。 –

12

Swift 3。0

// UIView的擴展

fileprivate var ActivityIndicatorViewAssociativeKey = "ActivityIndicatorViewAssociativeKey" 
public extension UIView { 
    var activityIndicatorView: UIActivityIndicatorView { 
     get { 
      if let activityIndicatorView = getAssociatedObject(&ActivityIndicatorViewAssociativeKey) as? UIActivityIndicatorView { 
       return activityIndicatorView 
      } else { 
       let activityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) 
       activityIndicatorView.activityIndicatorViewStyle = .gray 
       activityIndicatorView.color = .gray 
       activityIndicatorView.center = center 
       activityIndicatorView.hidesWhenStopped = true 
       addSubview(activityIndicatorView) 

       setAssociatedObject(activityIndicatorView, associativeKey: &ActivityIndicatorViewAssociativeKey, policy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 
       return activityIndicatorView 
      } 
     } 

     set { 
      addSubview(newValue) 
      setAssociatedObject(newValue, associativeKey:&ActivityIndicatorViewAssociativeKey, policy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC) 
     } 
    } 
} 

// NSObject的擴展

public extension NSObject { 
    func setAssociatedObject(_ value: AnyObject?, associativeKey: UnsafeRawPointer, policy: objc_AssociationPolicy) { 
     if let valueAsAnyObject = value { 
      objc_setAssociatedObject(self, associativeKey, valueAsAnyObject, policy) 
     } 
    } 

    func getAssociatedObject(_ associativeKey: UnsafeRawPointer) -> Any? { 
     guard let valueAsType = objc_getAssociatedObject(self, associativeKey) else { 
      return nil 
     } 
     return valueAsType 
    } 
} 

開始動畫

tableView.activityIndicatorView.startAnimating()

停止動畫

tableView.activityIndicatorView.stopAnimating()

可以在Magic

+0

無法讓它在Swift 4中作爲自己的擴展文件工作。 –

0

另一種方法找到更多的代碼,在我的代碼我增加了一個擴展的UITableView(雨燕2.3):

extension UITableView { 
    func activityIndicator(center: CGPoint = CGPointZero, loadingInProgress: Bool) { 
     let tag = 12093 
     if loadingInProgress { 
      var indicator = UIActivityIndicatorView() 
      indicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 40, 40)) 
      indicator.tag = tag 
      indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge 
      indicator.color = //Your color here 
      indicator.center = center 
      indicator.startAnimating() 
      indicator.hidesWhenStopped = true 
      self.superview?.addSubview(indicator) 
     }else { 
      if let indicator = self.superview?.viewWithTag(tag) as? UIActivityIndicatorView { { 
       indicator.stopAnimating() 
       indicator.removeFromSuperview() 
      } 
     } 
    } 
} 

注意:我的tableview被嵌入在一個UIView(上海華)

1
func setupSpinner(){ 
    spinner = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height:40)) 
    spinner.color = UIColor(Colors.Accent) 
    self.spinner.center = CGPoint(x:UIScreen.main.bounds.size.width/2, y:UIScreen.main.bounds.size.height/2) 
    self.view.addSubview(spinner) 
    spinner.hidesWhenStopped = true 
} 
0

使用「lazy var」。它比功能更好

fileprivate lazy var activityIndicatorView: UIActivityIndicatorView = { 
    let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray) 
    activityIndicatorView.hidesWhenStopped = true 

    // Set Center 
    var center = self.view.center 
    if let navigationBarFrame = self.navigationController?.navigationBar.frame { 
     center.y -= (navigationBarFrame.origin.y + navigationBarFrame.size.height) 
    } 
    activityIndicatorView.center = center 

    self.view.addSubview(activityIndicatorView) 
    return activityIndicatorView 
}() 

剛開始的任何地方

這樣

func requestData() { 
    // Request something 
    activityIndicatorView.startAnimating() 
} 
相關問題