0

我有一个显示两分钟的视图(启动画面):

- (void)applicationDidFinishLaunching:(UIApplication *)application

{

[viewController showSplash];

}

- (void)showSplash // Show splash screen
{
 UIViewController *modalViewController = [[UIViewController alloc] init];
 modalViewController.view = modelView;
 [self presentModalViewController:modalViewController animated:NO];
 [self performSelector:@selector(hideSplash) withObject:nil afterDelay:120.0]; 
}

我想在这个初始屏幕上添加一个从 2 分钟倒计时到零的计时器。

我想我需要创建另一个包含计时器的视图。这个对吗?我将如何执行此操作并将其添加到启动屏幕,以及如何使计时器中的数字以白色显示在屏幕上?

我知道两分钟显示启动画面的时间很长……但我只是在尝试各种事情,这两分钟还有其他事情发生!

非常感谢,

斯图

编辑 // 好的,我现在有了这个:

(。H)

NSTimer *timer5;
    UILabel *countDown;
    float timeOnSplash;

(.m)

- (void) updateLabel:(NSTimer*)theTimer 
{
    float timeOnSplash = timeOnSplash - 1;
    countDown.text = [NSString stringWithFormat:@"%02d:%02d", timeOnSplash];
}


- (void)applicationDidFinishLaunching:(UIApplication *)application

{

    timer5 = [NSTimer scheduledTimerWithTimeInterval:1

        target:self
        selector:@selector(updateLabel:) 
        userInfo:nil
        repeats:YES];

    countDown.text = [NSString stringWithFormat:@"%02d:%02d", timeOnSplash];
}

运行代码时出现以下未捕获的异常:

'NSUnknownKeyException',原因:'[setValue:forUndefinedKey:]:此类不符合键 countDown 的键值编码。

有任何想法吗?

编辑 2 // 现在工作,有关出色的解决方案,请参阅 TechZen 的答案。

非常感谢大家!

4

4 回答 4

3

NSTimers 是不寻常的对象,因为它们不附加到创建它们的对象,而是附加到应用程序 NSRunLoop 实例。如果计时器是一次性的,则您不必保留对它的任何引用。您应该启动计时器并忘记它。

在您的情况下,您需要两个跟踪两个时间间隔(1)每秒的过去,以便您可以更新界面和(2)两分钟的时间间隔总计。

对于 (1),您应该调用一个间隔为一秒的重复计时器,该计时器调用 modalviewcontroller 中更新接口的方法。调用计时器的最佳位置是控制器的viewDidAppear方法。对于(2),您可以拥有存储值 159 的控制器的属性,然后让计时器调用的方法在每次调用该方法时递减它。当它达到零时,它会使计时器无效。

您应该知道,计时器会受到 runloop 处理所有事件的速度的影响。如果您有密集的后台进程,并且不会每隔几微秒暂停一次,则计时器可能无法按时触发。如果你遇到这个问题,你应该考虑为闪屏和配置创建一个单独的线程。

我确实想知道为什么您需要将启动画面显示整整两分钟。


顺便说一句,iPhone 人机界面指南明确规定您不应使用启动画面。它们可能会导致您的应用被拒绝。使用长时间悬停的闪屏给人的印象是应用程序或手机失败了,Apple 也不喜欢这样。

如果您在应用程序可用之前需要进行一些繁重的配置,最好创建一个显示正在进行的配置的界面。这样,很明显该应用程序正在运行,而不仅仅是挂起。

更好的是,因为移动中的任何人都不想盯着静态的 iPhone 应用程序看两分钟,所以最好让您的用户开始在一个线程中做某事,而应用程序在另一个线程中进行配置。例如,在某种 url 连接中,您可以让用户在应用程序建立连接时输入一些数据和地址。对于游戏,您可以让用户选择他们的用户名、查看高分、查看说明等。

您应该在设计过程中记住人们在 iPhone 上使用应用程序主要是因为它可以节省他们的时间。他们没有拖出笔记本电脑或去电脑执行某些任务。您的应用程序设计应专注于让用户的任务尽快执行。即使在游戏中也是如此。

通常,我会警告不要过早优化,但这是一件大事。

编辑01:

你想要在你的闪屏控制器中有这样的东西。

@interface SplashScreenViewController : UIViewController {
    NSInteger countDown;
    IBOutlet UILabel *displayTimeLabel;
}
@property NSInteger countDown;
@property(nonatomic, retain)   UILabel *displayTimeLabel;
@end

#import "SplashScreenViewController.h"

@implementation SplashScreenViewController
@synthesize countDown;
@synthesize displayTimeLabel;

-(void) viewDidAppear:(BOOL)animated{
    countDown=120;
    NSTimer *secTimer=[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateCountDown:) userInfo:nil repeats:YES];
}//------------------------------------viewDidAppear:------------------------------------


-(void) updateCountDown:(NSTimer *) theTimer{
    NSInteger mins,secs;
    NSString *timeString;
    countDown--;
    if (countDown>=0) {
        mins=countDown/60;
        secs=countDown%60;
        displayTimeLabel.text=[NSString stringWithFormat:@"%02d:%02d",mins,secs];

    } else {
        [theTimer invalidate];
        // do whatever you wanted to do after two minutes
    }

}//-------------------------------------(void) updateCountDown------------------------------------
@end
-------
于 2009-12-06T15:55:29.923 回答
0

您可以按照@Morion 的建议使用 NSTimer。一种替代方法如下:

在您的 @interface 文件中,添加变量:

NSInteger   countDown;

然后在@implementation:

- (void)showSplash // Show splash screen
{
    UIViewController *modalViewController = [[UIViewController alloc] init];
    modalViewController.view = modelView;
    [self presentModalViewController:modalViewController animated:NO];
    countdown = 120;
    [self performSelector:@selector(updateTime) withObject:nil afterDelay:1.0];
}


- (void)updateTime {
    //decrement countDown
    if(--countDown > 0){
        //
        // change the text in your UILabel or wherever...
        //
        //set up another one-second delayed invocation
        [self performSelector:@selector(updateTime) withObject:nil afterDelay:1.0];

    }else{
        // finished
        [self hideSplash];
    }
}
于 2009-12-06T12:44:39.633 回答
0

是的,一种方法是创建一个具有透明背景 ( [UIColor clearColor]) 和带有白色文本标签的新视图。只需调用[modalViewController.view addSubview:timerView]将其添加为子视图/覆盖。

我不确定在实际创建视图和设置标签等方面需要多少帮助。

于 2009-12-06T12:45:24.967 回答
0

您可以使用 NSTimer 每秒调用一些方法。在那里您可以更改模态 ViewController 的视图(例如,更改标签上的文本,这将显示时间)。当您的方法将被调用 120 次时,您只需使计时器无效

于 2009-12-06T11:50:55.953 回答