2010-12-22 67 views
2

我正在尝试制作一个非常简单的游戏,并且我被卡住了。基本上我想让UIImageView每2秒出现一次。我的问题是我无法跟踪累计时间。现在我有这个:目标C:不断调用函数

NSDate *date = [NSDate date]; 

NSTimeInterval secondsSinceNow = [date timeIntervalSinceNow]; 

NSLog(@"date = %@", date); 
NSLog(@"secondsSinceNow = %f", secondsSinceNow); 

它在我的按钮功能,所以它被称为当用户点击按钮。它返回的小数始终小于1.我已经在viewDidLoad方法以及它自己的方法中尝试过它,但都没有成功。

我认为它会工作,如果它在自己的方法是不断检查,但我不知道该怎么做。

总之,我需要一个定时器/计数器,每秒更新一次。

回答

5
@interface className 
{ 
NSTimer * timer; 
} 

@property (nonatomic, retain) NSTimer * timer; 

@end 


@implementation className 
@synthesize timer; 

... 

/*工厂方法在此处得到纠正。通过复制和粘贴没有警告应该工作*/

-(void) applicationDidFinishLaunching : (UIApplication *) application { 
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES]; 

} 

//define the target method 

/*,因为它需要周围的NSTimer括号法修正*/

-(void) targetMethod: (NSTimer *) theTimer { 
NSLog(@"Me is here at 1 minute delay"); 
} 
.. 
@end 

从这里 http://www.iphonedevsdk.com/forum/iphone-sdk-development/14403-nstimer-examples.html

+0

工厂方法的名称是错误的,正确的工厂方法称为scheduledTimerWithTimeInterval:target:selector:userInfo:repeatats: – LeonS 2012-05-28 06:59:01

0

您可以简单地使用NSTimer以所需的两秒间隔调用班级中的选择器。

也就是说,您也可以使用CABasicAnimation来淡化UIImageView的不透明度,等待所需的效果。

0

采取了那些前两行会立即被调用,那么它(应该)总是小于一秒。日期正在被实例化,然后timeIntervalSinceNow被立即调用,当它们之间很少/没有时间发生时。目标是首先调用日期,然后调用timeIntervalSinceNow以获得大于0的时间。但是,这仍然没有创建更新计时器。