The Activity Indicator can be used to show the user that a task is in progress. In this tutorial we will display an Activity Indicator which we will start and stop spinning using buttons.
Open Xcode and create a new Single View Application. For product name, use ActivityIndicatorDemo and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices, and that the Use Storyboards checkbox is deselected and Use Automatic Reference Counting checkbox is selected.
First, let's create our User Interface. Drag 2 buttons to the main view and name the buttons start and stop. The view should look like this.
Go to ViewController.m and add the following property to the @interface section.
@interface ViewController ()
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
In the viewDidLoad method we will add our Activity Indicator view to our main view.
self.view.backgroundColor = [UIColor blackColor];
// Do any additional setup after loading the view, typically from a nib.
CGRect frame = CGRectMake (120.0, 185.0, 80, 80);
self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:frame];
We make the backgroundcolor of our view black so the Activity Indicator is better visible. Note the Activity Indicator is only visible when the animation starts. Add two IBAction method to start and stop the animation
Connect the action methods from our buttons in Interface Builder. Build and Run, press the buttons to start and stop the animation.
You can download the source code of the ActivityIndicatorDemo at the ioscreator repository on github.