Posted By

zopebie on 08/10/12


Tagged

ios uiprogressview UIActivityIndicatorView


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

zopebie


Progress and Activity Indicators


 / Published in: Objective C
 

When performing tasks that may take some time, you often need to provide some kind of visual feedback to your users. If you know how long the task will take to complete, you can use a progress indicator to show the user how much of the task has been performed and how much still has to run. If you are unable to determine the duration of the task, use a “busy” indicator (such as the beach ball or hourglass on OS X).

iOS provides classes for showing both progress and activity.

  1. // Showing progress
  2.  
  3. NSTimer *timer;
  4.  
  5. - (void)updateProgress:(NSTimer *)sender
  6. {
  7. UIProgressView *progress = [sender userInfo];
  8.  
  9. //have we completed?
  10. if (progress.progress == 1.0)
  11. [timer invalidate];
  12. else
  13. progress.progress += 0.05;
  14. }
  15.  
  16. - (void)viewDidLoad {
  17.  
  18. [super viewDidLoad];
  19.  
  20. UIProgressView *myProgressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
  21.  
  22. CGRect progressFrame = CGRectMake(10,100,300,25);
  23. [myProgressView setFrame:progressFrame];
  24.  
  25. [myProgressView setProgress:0.0];
  26.  
  27. [self.view addSubview:myProgressView];
  28.  
  29. //create timer
  30. timer = [[NSTimer scheduledTimerWithTimeInterval:0.1
  31. target:self
  32. selector:@selector(updateProgress:)
  33. userInfo:myProgressView
  34. repeats:YES] retain];
  35. }
  36.  
  37.  
  38. // Showing Activity
  39.  
  40.  
  41. - (void)viewDidLoad {
  42.  
  43. [super viewDidLoad];
  44.  
  45. [self.view setBackgroundColor:[UIColor blackColor]];
  46.  
  47. UIActivityIndicatorView *myActivityView = [[UIActivityIndicatorView alloc]
  48. initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  49.  
  50. CGRect activityFrame = CGRectMake(130,100,50,50);
  51. [myActivityView setFrame:activityFrame];
  52. myActivityView.hidesWhenStopped = YES;
  53. [myActivityView startAnimating];
  54.  
  55. [self.view addSubview:myActivityView];
  56.  
  57. }

Report this snippet  

You need to login to post a comment.