Monday, August 05, 2013

iOS Tip: Displaying the Activity Indicator View

If your application is performing some long-runing task, it is often good UI design to offer feedback to the user. One of the easiest way is to display a spinner (formally known as the Activity Indicator View) that animates by, what else, spinning. You can display the spinner either within your application, or in the status bar of the iOS device.  

Assuming you have dragged and dropped a Activity Indicator View onto the View window and create an outlet for it named activityIndicator, you can set the following property in the viewDidLoad event:

- (void)viewDidLoad
    [super viewDidLoad];

    //---make the view disappear when you stop 
    // animating it---
    self.activityIndicator.hidesWhenStopped = YES;

The hidesWhenStopped property indicates if the Activity Indicator View should be hidden when it has stopped animating. In this case, you set it to YES so that it can automatically be hidden when you asked it to stop animating.

To make the Activity Indicator View animate, call the startAnimating method:

    //---start the animation---
    [self.activityIndicator startAnimating];
If you want the Activity Indicator View to appear in the status bar, use the networkActivityIndicatorVisible property of the UIApplication object and set it to YES:

    //---make the activity indicator appear in the
    // status bar---
    [UIApplication sharedApplication].
        networkActivityIndicatorVisible = YES;

To stop the Activity Indicator View from animating, call the stopAnimating method:

    //---stop the animation---
    [self.activityIndicator stopAnimating];
To stop the Activity Indicator View in the status bar, set the networkActivityIndicatorVisible property to NO:

    //---make the activity indicator disappear
    // in the status bar---
    [UIApplication sharedApplication].
        networkActivityIndicatorVisible = NO;

No comments: