How to show activity indicator while tableView loa

2020-05-15 07:38发布

When I switch between my tabs it loads some seconds and I want to know that my data is loading. For that I decided to add an activity indicator.

I wrote a little function:

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()
    }
}

that will show my indicator. And try to use it when I tapped from my infoController to button:

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

I show it before segue perform and hide after. It doesn't help me. When I did try to use sync:

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

But it doesn't help too. When I press to tab it opacity becomes 0.5 and I wait while it loading. But I do not see my activity indicator.

What is the problem?

12条回答
欢心
2楼-- · 2020-05-15 07:40

SWIFT

Place this below your class:

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

Place this in your 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()

In my case, I am requesting json objects through a func request, so I placed this at the end of that function to remove the activity indicator once the data loads:

self.indicator.stopAnimating()
self.indicator.hidesWhenStopped = true
查看更多
地球回转人心会变
3楼-- · 2020-05-15 07:40
 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
}
查看更多
forever°为你锁心
4楼-- · 2020-05-15 07:41

Swift 3.0

// UIView Extension

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 Extension

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
    }
}

start animation

tableView.activityIndicatorView.startAnimating()

stop animation

tableView.activityIndicatorView.stopAnimating()

You can find more code in Magic

查看更多
相关推荐>>
5楼-- · 2020-05-15 07:43

Just try this:

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)    
}

And where you want to start animating

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

For stop:

indicator.stopAnimating()
indicator.hidesWhenStopped = true

Note: Avoid the calling of start and stop at the same time. Just give some conditions.

SWIFT : 4.2 Just try this:

var indicator = UIActivityIndicatorView()

func activityIndicator() {
    indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
    indicator.style = UIActivityIndicatorView.Style.gray
    indicator.center = self.view.center
    self.view.addSubview(indicator)   
}

And where you want to start animating

activityIndicator()
indicator.startAnimating()
indicator.backgroundColor = .white

For stop:

indicator.stopAnimating()
indicator.hidesWhenStopped = true
查看更多
男人必须洒脱
6楼-- · 2020-05-15 07:47

Swift 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()

        }
        ...
    }
查看更多
男人必须洒脱
7楼-- · 2020-05-15 07:47

In order to place the UIActivityIndicator in foreground, even over the eventual UITableViewController separators, I have adopted this solution.

  • I have add the UIActivityIndicator programmatically, and add it as a subview of my UINavigationController

    var activityIndicator: UIActivityIndicatorView!
    
    override func viewDidLoad() {
       super.viewDidLoad()
       // Code
       // .... omissis
    
       // Set activity indicator
       activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
       activityIndicator.color = UIColor.darkGray
       activityIndicator.center = tableView.center
       activityIndicator.hidesWhenStopped = true
       activityIndicator.stopAnimating()
       self.navigationController?.view.addSubview(activityIndicator)
    }
    
  • I have just start & stop it when needed (in my case):

    func animateActivityIndicator(_ sender: Any ) {
       guard let vc = sender as? UIViewController else { return }
       if let v = vc as? MyTableViewController {
           if v.activityIndicator.isAnimating {
               v.activityIndicator.stopAnimating()
           } else {
               v.activityIndicator.startAnimating()
           }
       }
       // Others UIViewController or UITableViewController follows...
       // all of them exhibits an activityIndicator variable
       // implemented programmatically or with the storyboard
    }
    

PS. My environment is Xcode 10.0, iOS 12.0

查看更多
登录 后发表回答