TheDeveloperBlog.com

Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

<< Back to SWIFT

Swift UIActivityIndicatorView Example

Use the UIActivityIndicatorView to show progress. An Activity Indicator means the program is doing something.
UIActivityIndicatorView. A Button is tapped. The program needs time to think, to process the important command. An Activity Indicator indicates this pause.
With this control, a "beach ball" or "in-progress" icon is displayed. Grey or white styles can be used. This is an indeterminate progress bar.
To begin, please open a Single View Application in Xcode. Then drag an Activity Indicator and a Button to the application window. These will be used for the tutorial.
Add Swift outlets. Next please control drag the Activity Indicator the ViewController.swift file. Add an outlet also for the Button.UIButton

Action triggered: To create a "tap" event handler for the button, add an Action Triggered event. Now we add some important Swift code.

IsAnimating: This returns true if the UIActivityIndicatorView is currently animating, and false if not.

StopAnimation: Call this to pause the animation of the Activity Indicator. No motion occurs when the control is paused.

StartAnimation: Begins animating the control. Use this to get the "spinning beach ball" effect in motion.

Example UIActivityIndicatorView: Swift // // ViewController.swift // ExampleTimeE // // ... // import UIKit class ViewController: UIViewController { @IBOutlet weak var simpleActivity: UIActivityIndicatorView! @IBOutlet weak var simpleButton: UIButton! override func viewDidLoad() { super.viewDidLoad() } @IBAction func actionTriggered(sender: AnyObject) { // See if it is animating. if simpleActivity.isAnimating() { // If currently animating, stop it. simpleActivity.stopAnimating() } else { // Begin. simpleActivity.startAnimating() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Run simulator. Let us check out our program. Perhaps it worked. When we run the iOS Simulator, the Button and Activity Indicator will appear. The Activity Indicator will not be animating.

However: When we tap (or click) the Button, the spinning beach-ball effect will begin, mesmerizing us.

An alternative. When the time required for a task can be estimated, and the time is not small, then a UIProgressView is likely a better user interface widget.UIProgressView
A review. Sometimes an Activity Indicator can make us be more patient for a program. But sometimes another notification method, like a Progress Bar, may be more appropriate.
© TheDeveloperBlog.com
The Dev Codes