Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Apple > iPhone

  • Print
  • + Share This
This chapter is from the book

Progress and Activity Indicators

When performing tasks that may take some time, you often need to provide some kind of visual feedback to your users. If you know how long the task will take to complete, you can use a progress indicator to show the user how much of the task has been performed and how much still has to run. If you are unable to determine the duration of the task, use a "busy" indicator (such as the beach ball or hourglass on OS X).

The iPhone SDK provides classes for showing both progress and activity.

Indicating progress

When you want to show the progress of a task, use UIProgressView, a very simple class, consisting of only two properties.

You create a progress view and set its style using the initWithProgressViewStyle: method:

UIProgressView *myProgressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];

The indicator appears as a horizontal bar that fills from left to right to show completion (Figure 4.17). This is controlled by the progress property, using a value between 0.0 (not started) and 1.0 (completed):

[myProgressView setProgress:0.33];
Figure 4.17

Figure 4.17 A progress view at 33 percent completion.

Although you set the frame of the progress view, the maximum height of a progress view is 9 pixels, so any larger value will be ignored.

Code Listing 4.15 shows an example of using UIProgressView with the progress updated in a timer to simulate a long-running task.


Code Listing 4.15 Updating the progress view.

Showing activity

For tasks of an indeterminate duration, you can use the UIActivityIndicatorView class, represented by an animated "spinner" graphic (Figure 4.18).

Figure 4.18

Figure 4.18 An activity indicator view.

Similar to a progress view, you can create an activity indicator using the initWithActivityIndicatorStyle: method:

UIActivityIndicatorView *myActivityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

The default size of an activity view is a 21-pixel square. If you use the UIActivityIndicatorViewStyleWhiteLarge style, this increases to a 36-pixel square.

Unlike the progress view, however, the frame property controls both the height and the width of the view. For activity views larger than 36 pixels, it's best to use the larger style so the image won't become pixelated.

The activity view will initially be invisible. Calling the startAnimating method shows the activity view and causes the spinner graphic to animate:

[myActivityView startAnimating];

Calling stopAnimating will stop the spinner animation, but you need to remember to set the hidesWhenStopped property if you want the activity view to hide (Code Listing 4.16).


Code Listing 4.16 Creating an activity indicator view.

  • + Share This
  • 🔖 Save To Your Account