iPhone - 带进度条的启动画面 [英] iPhone - Splash Screen with progress bar

查看:137
本文介绍了iPhone - 带进度条的启动画面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个SplashView,它在后台显示Default.png,在前面显示UIProgressBar。但是启动画面没有更新...



在我的视图控制器中,我首先加载启动视图,其中包含一个参数,我的初始化步数是多少,然后我开始一个第二个线程通过NSTimer,在每个初始化步骤之后,我告诉SplashView显示新的进度值。



理论上一切看起来都不错,但是在运行这个应用程序时,进度条是没有更新(启动画面的方法接收值,我可以在日志中看到它)。我也尝试添加usleep(10000);在两者之间给视图更新一点时间,而不是使用我直接在视图上绘制的进度条并调用[self setNeedsDisplay];但一切都行不通:/



我做错了什么?



感谢您的帮助!<这是一些代码:

 
SPLASHSCREEN:
- (id)initWithFrame:(CGRect)frame withStepCount:(int)stepCount {
if(self = [super initWithFrame:frame]){
//初始化代码

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];
}
返回自我;
}

- (无效)勾选{
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];
}
返回自我;
}

- (void)finishInitialization {
//做一些事情,比如分配,打开数据库,创建视图,重要的东西......
[splashView蜱]; //这应该更新进度条...

//做一些事情,比如分配,打开数据库,创建视图,重物......
[splashView tick]; //这应该更新进度条...

// init done ...设置正确的视图并释放SplashView
}


解决方案

正如另一个答案所述,在一段有限的时间内,当您的应用程序正在启动时,会显示Default.png而您没有控制它。但是,如果在AppDelegate中创建了一个显示相同Default.png的新视图,则可以创建从原始Default.png到可以添加进度条的视图的无缝转换。



现在,您可能已经创建了一个视图或类似视图,并且每隔一段时间就会更新一个进度条,以便为用户提供一些反馈。这里的挑战是,只有在调用drawRect时才会绘制视图。但是,如果您从AppDelegate转到某个初始化代码到viewcontroller的viewDidLoad,而没有运行循环有机会找出需要调用drawRect的视图,那么您的视图将永远不会显示其状态栏。



因此,为了实现您想要的目标,您必须确保调用drawRect,例如将大量初始化代码推送到其他线程或计时器任务中,或者您可以在设置上下文之后通过调用drawRect来强制绘图。



如果您使用后台任务,那么请确保您的初始化代码是线程安全的。


I tried to create a SplashView which display the Default.png in the background and a UIProgressBar in front. But the splash screen is not being updated...

Inside my view controller I load first the splash view with a parameter how many steps my initialisation has and then I start a second thread via NSTimer and after each initialisation step I tell the SplashView to display the new progress value.

All looks good in theory, but when running this app the progress bar is not being updated (the method of the splash screen receives the values, I can see it in the logs). I also tried to add usleep(10000); in between to give the view updates a bit time and also instead of using the progress bar I drew directly on the view and called [self setNeedsDisplay]; but all didn't work :/

What am I doing wrong?

Thanks for your help!

Tom

Here is some 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
}

解决方案

As mentioned in another answer, for some finite amount of time, as your app is being launched, Default.png is displayed and you have no control over it. However, if in your AppDelegate, you create a new view that displays the same Default.png, you can create a seamless transition from the original Default.png to a view that you can add a progress bar to.

Now, presumably, you have created a view or similar and you are updating a progress bar every so often in order to give the user some feedback. The challenge here is that your view is only drawn when it gets called to do a drawRect. If, however, you go from AppDelegate to some initialization code to a viewcontroller's viewDidLoad, without the run loop getting a chance to figure out which views need to have drawRect called on, then your view will never display its status bar.

Therefore in order to accomplish what you want, you have to either make sure that drawRect gets called, such as by pushing off a lot of your initialization code into other threads or timer tasks, or you can force the drawing by calling drawRect yourself, after setting up contexts and such.

If you go with the background tasks, then make sure your initialization code is thread-safe.

这篇关于iPhone - 带进度条的启动画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆