You control when an activity indicator animates by calling the startAnimating() and stopAnimating() methods. To automatically hide the activity indicator when animation stops, set the hidesWhenStopped property to true.Apple Documentation

Steps:

  1. Create a new Xcode Project for (Single View Application).
  2. Give Project a name ( Lets say ActivityIndicatorDemo).
  3. Click next button and the Project will be created.
  4. Now go to Main StoryBoard of the Project and In the Object Library, select the “ActivityIndicator, and two  button” object and drag it into the view. And make the Proper IBOutlet connection and Action for Button.

    After Connectimg the IBOutlet and writing the delegates Your View Controller will Look something Like this

    #import “ViewController.h”

    @interface ViewController ()

    @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicatorView;

    @end

    @implementation ViewController

    – (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    }

    – (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    }

    – (IBAction)startActivityIndicator:(id)sender {

    }

    – (IBAction)stopActivityIndicator:(id)sender {

    }

    @end

    5. Now  we have to just call startAnimating and stopAnimating methods. Therse are the predefined methods of UIActivityIndicatorView.

    – (IBAction)startActivityIndicator:(id)sender {

    _activityIndicatorView.transform = CGAffineTransformMakeScale(2.0, 2.0);

    [self.activityIndicatorView startAnimating];

    }

    – (IBAction)stopActivityIndicator:(id)sender {

    _activityIndicatorView.transform = CGAffineTransformIdentity;

    [self.activityIndicatorView stopAnimating];

    }

    6. Now run the code and see the desired output. Enjoy Coding 🙂

Advertisements