When we draw the image, we want to display the activity indicator. Can anyone help us?
This is the correct way:
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
[self.view addSubview: activityIndicator];
[activityIndicator startAnimating];
[activityIndicator release];
Setup your autoresizing mask if you support rotation. Cheers!!!
Try this way
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(10.0, 0.0, 40.0, 40.0);
activityIndicator.center = super_view.center;
[super_view addSubview: activityIndicator];
[activityIndicator startAnimating];
Code updated for Swift 3
Swift code
import UIKit
class ViewController: UIViewController {
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
@IBOutlet weak var myBlueSubview: UIView!
@IBAction func showButtonTapped(sender: UIButton) {
activityIndicator.startAnimating()
}
@IBAction func hideButtonTapped(sender: UIButton) {
activityIndicator.stopAnimating()
}
override func viewDidLoad() {
super.viewDidLoad()
// set up activity indicator
activityIndicator.center = CGPoint(x: myBlueSubview.bounds.size.width/2, y: myBlueSubview.bounds.size.height/2)
activityIndicator.color = UIColor.yellow
myBlueSubview.addSubview(activityIndicator)
}
}
Notes
You can center the activity indicator in middle of the screen by adding it as a subview to the root view
rather than myBlueSubview
.
activityIndicator.center = CGPoint(x: self.view.bounds.size.width/2, y: self.view.bounds.size.height/2)
self.view.addSubview(activityIndicator)
You can also create the UIActivityIndicatorView
in the Interface Builder. Just drag an Activity Indicator View onto the storyboard and set the properties. Be sure to check Hides When Stopped. Use an outlet to call startAnimating()
and stopAnimating()
. See this tutorial.
Remember that the default color of the LargeWhite
style is white. So if you have a white background you can't see it, just change the color to black or whatever other color you want.
activityIndicator.color = UIColor.black
A common use case would be while a background task runs. Here is an example:
// start animating before the background task starts
activityIndicator.startAnimating()
// background task
DispatchQueue.global(qos: .userInitiated).async {
doSomethingThatTakesALongTime()
// return to the main thread
DispatchQueue.main.async {
// stop animating now that background task is finished
self.activityIndicator.stopAnimating()
}
}