0

I'm new to coding and I'm currently making a guide/reference app (My first app).

I've been using the interface builder to do most of the work. I know I need to use code soon but for now I enjoy learning with IB.

Here's my question: I have a view that has a lot of HD pictures, it takes 4-5 seconds to load until I can scroll the page smoothly. I want to add a progress view bar (between a UITableView and the navigation bar) that show a 5 seconds progress in order to let the user know that its still loading(I know about the activity indicator but the progress view bar looks nicer and seems easier to use).

Can someone guide me through all the steps in order to make a progress view bar act as a 5 seconds timer?

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
17yrsDev
  • 215
  • 1
  • 2
  • 13

2 Answers2

2

Let's implement as this way for 5 min progressView,

In .h file,

NSTimer * timer;

UIProgressView * progView;

float duration;

In .m file

- (void)viewDidLoad
{
    [super viewDidLoad];

    progView = [[UIProgressView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 10.0)];

    [self.view addSubview:progView];

    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];;
}

-(void)updateProgress
{
    duration += 0.1;

    progView.progress = (duration/5.0); // here 5.0 indicates your required time duration

    if (progView.progress == 1)
    {
        [timer invalidate];

        timer = nil;
    }
}

Thanks!

Natarajan
  • 3,241
  • 3
  • 17
  • 34
0

This should get you started:

@property (nonatomic, strong) NSTimer *progressTimer;
@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, strong) UIProgressView *progressView;

- (void)yourMethod
{
    if (!self.progressView)
    {
         self.progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 64, 320, 2)];
         self.progressView.progressTintColor = [UIColor greenColor];

         [self.navigationController.navigationBar.superview insertSubview:self.progressView belowSubview:self.navigationController.navigationBar];
    }
    else
    {
         self.progressView.hidden = NO;
    }

    self.progressTimer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(updateProgressView) userInfo:nil repeats:YES];

    NSTimer *stopProgressTimer = [NSTimer timerWithTimeInterval:5.0 target:self selector:@selector(stopProgressView) userInfo:nil repeats:NO];

    [stopProgressTimer fire];
}

- (void)stopProgressView
{
    [self.progressTimer invalidate];
    self.progress = 0.0f;
    self.progressView.hidden = YES;
    self.progressView.progress = 0.0f;
}

- (void)updateProgressView
{
    self.progress = (self.progress + 0.1f) / 5;
    self.progressView.progress = self.progress;
}
klcjr89
  • 5,862
  • 10
  • 58
  • 91