可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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 in same time. Just give some conditions.
回答2:
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
回答3:
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() } ... }
回答4:
This code can help you :)
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()
回答5:
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
回答6:
Another approach, In my code I added an extension for UITableView (Swift 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() } } } }
Note : My tableview is embedded in a UIView (superview)
回答7:
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 }
回答8:
Using "lazy var". It's better than function
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 }()
Just start the spinner anywhere
like this
func requestData() { // Request something activityIndicatorView.startAnimating() }