Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Objective C/iPhone中计时?_Ios_Iphone_Objective C - Fatal编程技术网

Ios 在Objective C/iPhone中计时?

Ios 在Objective C/iPhone中计时?,ios,iphone,objective-c,Ios,Iphone,Objective C,我正在制作一个Simon Says应用程序来了解更多关于Objective C的信息 我的SimonSaysViewController有4个按钮。当模式显示给用户时,他们的图像需要相应地改变 固定时间间隔计时器绝对可以 我就是找不到一个例子 我基本上需要一种设置,如: 当我可以进行图像交换逻辑时,TimerChecked回调 理想情况下,TimerChecked方法将是我的SimonSaysViewController的一种方法 @property (strong, nonatomic) NS

我正在制作一个Simon Says应用程序来了解更多关于Objective C的信息

我的SimonSaysViewController有4个按钮。当模式显示给用户时,他们的图像需要相应地改变

固定时间间隔计时器绝对可以

我就是找不到一个例子

我基本上需要一种设置,如:

当我可以进行图像交换逻辑时,TimerChecked回调

理想情况下,TimerChecked方法将是我的SimonSaysViewController的一种方法

@property (strong, nonatomic) NSTimer *tickTockTimer;
如何做到这一点


谢谢

这种东西通常对我有用

NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:2.0]; // 2 sec from now
NSTimer *self.timer = [[NSTimer alloc] initWithFireDate:fireDate interval:5 target:self selector:@selector(timerDidTick) userInfo:nil repeats:YES]; // fire 5 sec apart
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.timer forMode:NSDefaultRunLoopMode];

这种事情通常对我有用

NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:2.0]; // 2 sec from now
NSTimer *self.timer = [[NSTimer alloc] initWithFireDate:fireDate interval:5 target:self selector:@selector(timerDidTick) userInfo:nil repeats:YES]; // fire 5 sec apart
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.timer forMode:NSDefaultRunLoopMode];

这种事情通常对我有用

NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:2.0]; // 2 sec from now
NSTimer *self.timer = [[NSTimer alloc] initWithFireDate:fireDate interval:5 target:self selector:@selector(timerDidTick) userInfo:nil repeats:YES]; // fire 5 sec apart
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.timer forMode:NSDefaultRunLoopMode];

这种事情通常对我有用

NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:2.0]; // 2 sec from now
NSTimer *self.timer = [[NSTimer alloc] initWithFireDate:fireDate interval:5 target:self selector:@selector(timerDidTick) userInfo:nil repeats:YES]; // fire 5 sec apart
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.timer forMode:NSDefaultRunLoopMode];

斯泰默是你的朋友!将NSTimer属性添加到SimonSaysViewController

@property (strong, nonatomic) NSTimer *tickTockTimer;
根据您希望计时器启动的时间,您需要设置计时器。假设您希望在视图首次出现时启动计时器:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.tickTockTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}
然后执行
timerFired
方法并在那里执行您需要的操作

- (void)timerFired:(NSTimer *)timer {
    //change the image.
}
完成后别忘了使计时器失效

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
}

斯泰默是你的朋友!将NSTimer属性添加到SimonSaysViewController

@property (strong, nonatomic) NSTimer *tickTockTimer;
根据您希望计时器启动的时间,您需要设置计时器。假设您希望在视图首次出现时启动计时器:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.tickTockTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}
然后执行
timerFired
方法并在那里执行您需要的操作

- (void)timerFired:(NSTimer *)timer {
    //change the image.
}
完成后别忘了使计时器失效

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
}

斯泰默是你的朋友!将NSTimer属性添加到SimonSaysViewController

@property (strong, nonatomic) NSTimer *tickTockTimer;
根据您希望计时器启动的时间,您需要设置计时器。假设您希望在视图首次出现时启动计时器:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.tickTockTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}
然后执行
timerFired
方法并在那里执行您需要的操作

- (void)timerFired:(NSTimer *)timer {
    //change the image.
}
完成后别忘了使计时器失效

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
}

斯泰默是你的朋友!将NSTimer属性添加到SimonSaysViewController

@property (strong, nonatomic) NSTimer *tickTockTimer;
根据您希望计时器启动的时间,您需要设置计时器。假设您希望在视图首次出现时启动计时器:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.tickTockTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}
然后执行
timerFired
方法并在那里执行您需要的操作

- (void)timerFired:(NSTimer *)timer {
    //change the image.
}
完成后别忘了使计时器失效

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
}


这是一个使用NSTimer类的好例子:这是一个使用NSTimer类的好例子:这是一个使用NSTimer类的好例子:这是一个使用NSTimer类的好例子:是的,这段代码就是您需要的Milo。Lance,请添加[self.ticktoctimer invalidate],因为它是NSTimer中非常重要的一部分。请您解释一下强非原子的作用以及为什么您不管理内存?谢谢,更新了我的答案,包括
invalidate
哦,看起来是您添加的。现代objective c中的内存管理是由ARC系统处理的,对此的解释太长,无法对此进行评论。我建议研究objective c和自动引用计数(ARC)中的属性声明。是的,这段代码就是您需要的Milo。Lance,请添加[self.ticktoctimer invalidate],因为它是NSTimer中非常重要的一部分。请您解释一下强非原子的作用以及为什么您不管理内存?谢谢,更新了我的答案,包括
invalidate
哦,看起来是您添加的。现代objective c中的内存管理是由ARC系统处理的,对此的解释太长,无法对此进行评论。我建议研究objective c和自动引用计数(ARC)中的属性声明。是的,这段代码就是您需要的Milo。Lance,请添加[self.ticktoctimer invalidate],因为它是NSTimer中非常重要的一部分。请您解释一下强非原子的作用以及为什么您不管理内存?谢谢,更新了我的答案,包括
invalidate
哦,看起来是您添加的。现代objective c中的内存管理是由ARC系统处理的,对此的解释太长,无法对此进行评论。我建议研究objective c和自动引用计数(ARC)中的属性声明。是的,这段代码就是您需要的Milo。Lance,请添加[self.ticktoctimer invalidate],因为它是NSTimer中非常重要的一部分。请您解释一下强非原子的作用以及为什么您不管理内存?谢谢,更新了我的答案,包括
invalidate
哦,看起来是您添加的。现代objective c中的内存管理是由ARC系统处理的,对此的解释太长,无法对此进行评论。我建议研究objective c中的属性声明和自动引用计数(ARC)。