IPhone - Screensaver with progress bar

I tried creating a SplashView that displays Default.png in the background and a UIProgressBar in front. But the screensaver is not updated ...

Inside my view controller, I first load the popup view with the parameter, how many steps my initialization has, and then I start the second thread through NSTimer, and after each initialization step I tell SplashView to display the new progress value.

Everything looks fine in theory, but when you run this application, the progress bar does not update (the splash screen method takes values, I see this in the logs). I also tried adding usleep (10000); meanwhile, to give the view a little time to update, and instead of using the progress bar, I drew directly on the view and called [self setNeedsDisplay]; but it didn’t work: /

What am I doing wrong?

Thank you for your help!

Tom

Here is the code:

SPLASHSCREEN:
- (id) initWithFrame: (CGRect) frame withStepCount: (int) stepCount {
    if (self = [super initWithFrame: frame]) {
        // Initialization code

        background = [[UIImageView alloc] initWithFrame: [self bounds]];
        [background setImage: [UIImage imageWithContentsOfFile: [NSString stringWithFormat: @ "% @ /% @", [[NSBundle mainBundle] resourcePath], @ "Default.png"]]];
        [self addSubview: background];

        progressView = [[UIProgressView alloc] initWithProgressViewStyle: UIProgressViewStyleBar];
        [progressView setFrame: CGRectMake (60.0f, 222.0f, 200.0f, 20.0f)];
        [progressView setProgress: 0.0f];

        stepValue = 1.0f / (float) stepCount;

        [self addSubview: progressView];
    }
    return self;
}

- (void) tick {
    value + = stepValue;
    [progressView setProgress: value];
}



VIEWCONTROLLER:

- (id) initWithNibName: (NSString *) nibNameOrNil bundle: (NSBundle *) nibBundleOrNil {
    if (self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]) {

        splashView = [[SplashView alloc] initWithFrame: CGRectMake (0.0f, 0.0f, 320.0f, 480.0f) withStepCount: 9];
        [self setView: splashView];

        NSTimer * delayTimer;
        delayTimer = [NSTimer scheduledTimerWithTimeInterval: 0.05 target: self selector: @selector (finishInitialization) userInfo: nil repeats: NO];
    }
    return self;
}

- (void) finishInitialization {
    // do some stuff, like allocation, opening a db, creating views, heavy stuff ...
    [splashView tick]; // this should update the progress bar ...

    // do some stuff, like allocation, opening a db, creating views, heavy stuff ...
    [splashView tick]; // this should update the progress bar ...

    // init done ... set the right view and release the SplashView
}
+3
source share
4 answers

, , , Default.png, . , AppDelegate , Default.png, Default.png , .

, , , , . , , , drawRect. , , AppDelegate viewDirectLoad viewcontroller, , , drawRect, .

, , , , drawRect , , , , drawRect , ..

, , .

+5

Default.png - , , . , applicationDidLaunch. "Splash Screen" ( , view ) , , .

+3

, . , ( , ), .

+1

The main thread, as far as I know, is the only one that can safely do things in the GUI, and its event loop (that is, the main thread of the application) is the one that performs the actual display after you called -setNeedsDisplay. Create a new thread to load and update the progress of the main thread.

+1
source

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


All Articles