Chr*_*ris 2 time counter date objective-c
我正试图制作一个非常简单的游戏而且我被卡住了.基本上我想每2秒钟出现一次UIImageView.我的问题是我无法跟踪累积时间.现在我有这个:
NSDate *date = [NSDate date];
NSTimeInterval secondsSinceNow = [date timeIntervalSinceNow];
NSLog(@"date = %@", date);
NSLog(@"secondsSinceNow = %f", secondsSinceNow);
Run Code Online (Sandbox Code Playgroud)
它在我的按钮功能中,因此当用户点击按钮时会调用它.它返回一个始终小于1的十进制数.我在viewDidLoad方法中尝试了它以及它自己的方法,但都没有工作.
我认为如果它自己的方法不断检查它会起作用,但我不知道该怎么做.
简而言之,我需要一个每秒更新一次的计时器/计数器.
@interface className
{
NSTimer * timer;
}
@property (nonatomic, retain) NSTimer * timer;
@end
@implementation className
@synthesize timer;
...
Run Code Online (Sandbox Code Playgroud)
/*工厂方法在这里得到纠正.通过复制和粘贴*/应该没有警告工作
-(void) applicationDidFinishLaunching : (UIApplication *) application {
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES];
}
//define the target method
Run Code Online (Sandbox Code Playgroud)
/*方法已更正,因为它需要围绕NSTimer的括号*/
-(void) targetMethod: (NSTimer *) theTimer {
NSLog(@"Me is here at 1 minute delay");
}
..
@end
Run Code Online (Sandbox Code Playgroud)
取自 http://www.iphonedevsdk.com/forum/iphone-sdk-development/14403-nstimer-examples.html