UIActivityIndicatorView

This app remains motionles for three seconds, rotates the activity indicator for 10 seconds, and then stops. This sequence is stage managed by the view controller. The view controller manipulates the view like a marionette. The strings are the startAnimating and startAnimating methods of the view.

Source code in Activity.zip

  1. Class AppDelegate: unchanged.
  2. Class ViewController creates two NSTimers.
  3. Class View

The activity indicator

Like the UISwitch we saw here, we do not have to specify a size for an UIActivityIndicatorView. On iPhone 6, it assumes a default size of 20 × 20 pairs of pixels for UIActivityIndicatorViewStyle.Gray, 37 × 37 for UIActivityIndicatorViewStyle.LargeWhite.

The gear has 12 teeth. The highlight starts at 9 o’clock and rotates clockwise at the rate of 360° per second. Hide the activity indicator when it has finished so the user won’t think the app has stalled. See Activity Indicator in the Human Interface Guidelines.

The timer

An NSTimer is like an invisible UIButton that is automatically pressed after a certain number of seconds. When the time is up, it can call a method of an object. Unlike the dispatch_after we saw next example.

Things to try

  1. Try a different style of activity indicator, e.g., UIActivityIndicatorViewStyle.LargeWhite. Warning: a UIActivityIndicatorViewStyle.White or LargeWhite indicator on a white background will be totally invisible.

  2. Set the color:
    		activityIndicatorView.color = UIColor.blueColor();
    

  3. Has there been any app in this course that kept the user waiting for something long enough to dispay an activity indicator? The best candidate was the temperature exercise in Hello. Start the activity indicator animating immediately before calling the resume method of the download task, and stop animating at the beginning of the completion handler passed to downloadTaskWithURL(_:completionHandler:).