在视图中添加可视计时器,iPhone
我有一个显示两分钟的视图(启动屏幕):在视图中添加可视计时器,iPhone,iphone,objective-c,timer,subview,Iphone,Objective C,Timer,Subview,我有一个显示两分钟的视图(启动屏幕): - (void)applicationDidFinishLaunching:(UIApplication *)application { [viewController showSplash]; } - (void)showSplash // Show splash screen { UIViewController *modalViewController = [[UIViewController alloc] init]; modalVie
- (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)
(.m)
我在运行代码时遇到以下未捕获的异常:
“NSUnknownKeyException”,原因:“[setValue:forUndefinedKey:]:此类不符合键倒计时的键值编码。”
有什么想法吗
编辑2//立即工作,有关优秀的解决方案,请参阅TechZen的答案
多谢大家 您可以使用NSTimer每秒调用某个方法。在那里,您可以更改模态ViewController的视图(例如,更改标签上的文本,它将显示时间)。当您的方法将被调用120次时,您只需使计时器无效您可以使用NSTimer每秒调用一个方法。在那里,您可以更改模态ViewController的视图(例如,更改标签上的文本,它将显示时间)。当您的方法将被调用120次时,您只需使计时器无效您可以按照@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];
}
}
您可以按照@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];
}
}
是的,一种方法是创建一个具有透明背景(
[UIColor clearColor]
)和白色文本标签的新视图。只需调用[modalViewController.view addSubview:timerView]
将其添加为子视图/覆盖
我不确定您在实际创建视图和设置标签等方面需要多少帮助。是的,一种方法是创建一个具有透明背景(
[UIColor clearColor]
)和白色文本标签的新视图。只需调用[modalViewController.view addSubview:timerView]
将其添加为子视图/覆盖
我不确定您在实际创建视图和设置标签等方面需要多少帮助。n计时器是不寻常的对象,因为它们不附着到创建它们的对象,而是附着到应用程序nsrunlop实例。如果计时器是一次性的,则不必保留对它的任何引用。你应该启动计时器,忘掉它 在您的情况下,您需要两个音轨两个时间间隔(1)每秒的传递,以便您可以更新界面和(2)两分钟间隔的传递 对于(1),您应该调用一个间隔为1秒的重复计时器,该计时器调用modalviewcontroller中更新接口的方法。触发计时器的最佳位置是控制器的
viewdide
方法。对于(2),您可以拥有控制器的属性,该属性存储159的值,然后让计时器调用的方法在每次调用该方法时将其递减。当它达到零时,计时器将失效
您应该知道,计时器受runloop处理所有事件的速度的影响。如果您有密集的后台进程,而不是每隔几微秒暂停一次,则计时器可能无法按时启动。如果您遇到这个问题,您应该考虑为启动屏幕和配置创建一个单独的线程。
我真想知道为什么你需要在启动屏幕上显示整整两分钟
另一方面,iPhone人机界面指南明确规定,您不应使用闪屏。它们可能会导致您的应用被拒绝。使用长时间挂在屏幕上的闪屏给人的印象是应用程序或手机出现了故障,苹果也不喜欢这样 如果在应用程序可用之前需要进行一些繁重的配置,那么最好创建一个界面来显示正在进行的配置。这样,很明显,该应用程序正在运行,而不仅仅是挂起 更好的是,因为移动中没有人想盯着一个静态的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
-------
NSTimer是不寻常的对象,因为它们不附加到创建它们的对象,而是附加到应用程序nsrunlop实例。如果计时器是一次性的,则不必保留对它的任何引用。你应该启动计时器,忘掉它 在您的情况下,您需要两个音轨两个时间间隔(1)每秒的传递,以便您可以更新界面和(2)两分钟间隔的传递 对于(1),您应该调用一个间隔为1秒的重复计时器,该计时器调用modalviewcontroller中更新接口的方法。最好的去处