Activity indicator for 5 seconds?

Everything

I try to start UIActivityIndicator for 5 seconds using the viewdidappear method. But how can I specify a time indicator for an activity indicator (5 seconds)?

This is the code for UIActivityIndicator

  UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; activityView.center=self.view.center; [activityView startAnimating]; [self.view addSubview:activityView]; 
+4
source share
4 answers

With one line and without additional dependencies and conditions:

 [activityView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:5.0]; 
+5
source

Set the type of activity to automatically hide when stopped

 activityView.hidesWhenStopped = YES; 

And hide it after 5s as follows:

 [activityView performSelector:@selector(stopAnimating) withObject:nil afterDelay:5.0]; 

This will allow you to start the animation again just by calling startAnimating if you need it in the future and keep a pointer to the UIActivityIndicatorView

If you just need to show the activity indicator once and will no longer use it, Alexanders answer best suits your needs.

+4
source

Use NSTimer for 5 seconds. In the timer callback method, stop the animation

0
source

try this code

  -(void)viewDidAppear:(BOOL)animated { UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; activityView.center=self.view.center; [self.view addSubview:activityView]; [activityView startAnimating]; activityView.tag=1111; [activityView release]; } [activityView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:5.0]; -(void)removeFromSuperview { UIActivityIndicatorView *activityView=(UIActivityIndicatorView*)[self.viewviewWithTag:1111]; [activityView stopAnimating]; [activityView removeFromSuperview]; } 
0
source

Source: https://habr.com/ru/post/1483755/


All Articles