In this recipe, we will learn how we can display the progress of any given length.
In this recipe, we will discuss the usage of the UIProgressBar
object and how we can use the associated properties to display an animated progress bar.
In order to see how this can be achieved, we need to modify the ViewObjectsExample application that we created in the previous recipe. Perform the following steps to do so:
ViewObjectsExample.xcodeproj
project file.ViewController.xib
file from the project navigator window.UIImageView
we added previously.ProgressViewcontrol
so that its width is as wide as the view.ProgressView
, and name it pgbProgress
.Our next step is to create the code functionality that will be responsible for displaying the progress and animating the bar:
ViewController.m
implementation file from the project navigator.#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize subView;
@synthesize lblInfo;
@synthesize pgbProgress;
@synthesize imgPicture;
@synthesize txtTextInput;
float incrementBy = 0.0;
btnPressMe
method, as shown in the highlighted line of the following code snippet:- (IBAction)btnTapHere:(UIButton *)sender {
[self fillProgressBar];
[self.lblInfo setBackgroundColor:[UIColor yellowColor]];
}
- (void)fillProgressBar { [self.pgbProgress setProgress:(incrementBy = 0.0f)]; [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(incrementBar:) userInfo:nil repeats:YES]; } -(void)incrementBar:(NSTimer *)timer { incrementBy += 10.0f; [pgbProgress setProgress:(incrementBy / 100)]; if (incrementBy > 100.0) { self.lblInfo.text = @"Processing has been Completed"; [timer invalidate]; } else { self.lblInfo.text = [NSString stringWithFormat:@"Processing data records: %3.2f", (pgbProgress.progress * 100)]; } }
When the compilation completes, the iOS Simulator will appear. Click on the Tap Here button to see the progress bar fill gradually, as shown in the following code snippet:
In the following line of code, we
declare a float
variable incrementBy
. This variable will be used by our method to gradually increment and fill the progress bar.
float incrementBy = 0.0;
Next, we modify our btnTapHere:
method to include a call to our method, fillProgressBarmethod
that will be responsible for handling the processing, as well as calling other methods to gradually fill the bar.
- (IBAction)btnTapHere:(UIButton *)sender {
[self fillProgressBar];
[self.lblInfo setBackgroundColor:[UIColor yellowColor]];
}
In our next code snippet, the fillProgressBar
method initializes the incrementBy
variable to 0
to make sure that the bar is not filled at all. We then use the scheduledTimerWithTimeInterval
method, which is one of the class methods of the NSTimer
class, which is used to create timers.
NSTimer
is an object that is scheduled to run within the run loop, so that it can check time passed periodically. When enough time has passed, it fires. If the timer has been set up to be non repeating, it then unschedules itself, otherwise it clears its elapsed time and waits to fire again. Next, we specify the duration in seconds when to call the incrementBar:
method, as specified by the selector
property and keep calling this method until the timer is invalidated.
- (void)fillProgressBar { [self.pgbProgress setProgress:(incrementBy = 0.0f)]; [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(incrementBar:) userInfo:nil repeats:YES]; }
In our final code snippet, we start by
setting our incrementBar
variable to the number of incremental steps this should take to fill our progress bar value, and then update the progress
property of our ProgressView
. Next, we check to see if we have reached our bar threshold and then update the label accordingly, before finally invalidating our timer
object to prevent the method from being called.
Alternatively, if we have not yet reached our threshold, we update the Label
object to show the progress bar's current progress.
-(void)incrementBar:(NSTimer *)timer { incrementBy += 10.0f; [pgbProgress setProgress:(incrementBy / 100)]; if (incrementBy > 100.0) { self.lblInfo.text = @"Processing has been Completed"; [timer invalidate]; } else { self.lblInfo.text = [NSString stringWithFormat:@"Processing data records: %3.2f", (pgbProgress.progress * 100)]; } }
If you would like to find out more information on the NSTimer
class, you can refer to the Apple Developer documentation, located at https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html.