rbk
rbk

Reputation: 283

UIActivityIndicator as long as to implement function

I want to use UIActivityIndicator on a function.

I'm implementing some Core Filters, some of which take almost a second to implement. I want that UIActivityIndicator to start and stop according the function.

I looked up online, but it's mostly using a timer. So that would make it hard-wired and not based on how long it actually takes to implement the function.

Can someone tell me a small example how I can do that ?

Upvotes: 1

Views: 56

Answers (2)

Ramdhas
Ramdhas

Reputation: 1765

Declare ActivityIndicator

@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;

then,

- (void)viewDidLoad 
{ 
  [super viewDidLoad];

  self.view.backgroundColor = [UIColor blackColor]; 

  // Do any additional setup after loading the view, typically from a nib. 
  CGRect frame = CGRectMake (120.0, 185.0, 80, 80);

  self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:frame];

  [self.view addSubview:self.activityIndicatorView];
}

using this code you can start and stop the activityIndicator

[self.activityIndicatorView startAnimating];  //start
[self.activityIndicatorView stopAnimating];   //stop

Upvotes: 1

Rajesh
Rajesh

Reputation: 10434

all UI animation and navigation would be performed once the method execution is over. So if you want such functionality go for timer or dispatch queue.

as follows

UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:self.window.frame];
    [activity startAnimating];
    //Your methods to be executed
    double delayInSeconds = 0.01;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [activity stopAnimating];
    });

in this method execution is kept in a queue after the execution of current method, method written dispatch_time_t will be executed.

Upvotes: 0

Related Questions