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?
willDisplayCell: used here for smoother UI (single cell usually displays fast after willDisplay: call). You could also try it with tableView:didEndDisplayingCell: . Much better for knowing when all the cells load that are visible. However this will be called whenever the user scrolls to view more cells.
Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With