Displaying the progress to the user

In this recipe, we will learn how we can display the progress of any given length.

Getting ready

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.

How to do it...

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:

  1. Open the ViewObjectsExample.xcodeproj project file.
  2. Select the ViewController.xib file from the project navigator window.
  3. From Object Library, drag a ProgressView object onto the subview and place it underneath the UIImageView we added previously.
  4. Resize the ProgressViewcontrol so that its width is as wide as the view.
  5. Next, create the outlet and property for this 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:

  1. Open the ViewController.m implementation file from the project navigator.
  2. Type in the variable declaration highlighted in the following code snippet:
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    @synthesize subView;
    @synthesize lblInfo;
    @synthesize pgbProgress;
    @synthesize imgPicture;
    @synthesize txtTextInput;
    
    float incrementBy = 0.0;
    
  3. Next, modify the btnPressMe method, as shown in the highlighted line of the following code snippet:
    - (IBAction)btnTapHere:(UIButton *)sender {
    
     [self fillProgressBar];
     [self.lblInfo setBackgroundColor:[UIColor yellowColor]];
    }
  4. Next, type in the following code snippet:
    - (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)];
     }
    }
  5. Then, build and run the application by selecting Product | Run from the Product menu, or alternatively by pressing command + R.

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:

How to do it...

How it works…

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)];
 }
}

Note

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.

There's more…

The UIProgressView class supports one more style. This can be specified by setting the progressViewStyle property to UIProgressViewStyleBar. With that change, the bar will look like the one, as can be seen when using Apple's Mail application.

See also

  • The Adding and customizing views recipe
  • The Using labels to display text recipe
  • The Obtaining user input through the use of buttons recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset