将视觉定时器添加到视图,iPhone

时间:2009-12-06 11:42:04

标签: iphone objective-c timer subview

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

- (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分钟到0倒计时到这个启动画面。

我想我需要创建另一个包含计时器的视图。它是否正确?我该怎么做并将它添加到初始屏幕,如何让计时器中的数字以白色显示在屏幕上?

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

非常感谢,

斯图

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

(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 个答案:

答案 0 :(得分:3)

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

在你的情况下你需要两个轨道两个时间间隔(1)每秒的通过,以便你可以更新界面和(2)两分钟间隔总和的通过。

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

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

我不得不想知道为什么你需要在两分钟内显示启动画面。


另外,iPhone人机界面指南明确规定您不应使用闪屏。他们可能会导致您的应用被拒绝。使用悬浮的长闪屏幕给人的印象是应用程序或手机失败,Apple也不喜欢这样。

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

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

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

通常,我会警告不要过早优化,但这有点大不了。

Edit01:

你需要在启动画面控制器中使用这样的东西。

@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
-------

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您可以使用NSTimer作为@Morion建议。另一种选择是:

在@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];
    }
}

答案 3 :(得分:0)

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

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