0

我有一个问题:我想让 NSTimer 等到 FLAG 变量为 YES,如果 FLAG = YES,则 myTimer 停止。我怎样才能做到这一点?我尝试了以下代码:

NSTimer *myTimer;
int delay = 6.0;
scanTimer= [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(anotherfunc) userInfo:nil repeats:YES];
myTimer= [NSTimer timerWithTimeInterval: delay
                                         target:self
                                       selector: @selector(resetAll:) userInfo:nil
                                        repeats:NO];
        [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSModalPanelRunLoopMode];
        [[NSApplication sharedApplication] runModalForWindow: scanningPanel];

这是 resetAll() 函数:

-(void) resetAll: (NSTimer *) theTimer
{
    if(FLAG)
    {
        NSLog(@"killWindow");
        [[NSApplication sharedApplication] abortModal];
        [scanningPanel orderOut: nil];
        FLAG = NO;
    }
    else
    {
        delay +=6.0;
        myTimer= [NSTimer timerWithTimeInterval: delay
                                         target:self
                                       selector: @selector(resetAll:) userInfo:nil
                                        repeats:NO];
        [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSModalPanelRunLoopMode];
        [[NSApplication sharedApplication] runModalForWindow: scanningPanel];
    }

}

我使用了 2 个 NSTimer,但只有 myTimer 运行,scanTimer 没有运行。请给我任何建议。提前致谢

4

3 回答 3

3

为什么不使用 Key Value Observing 进行 FLAG 更改?将标志变量添加为您正在使用的类的属性:

@property(nonatomic, assign) BOOL flag;

为避免在 .m 中出现重复位置:

#define kFooFlagKey @"flag"

覆盖 -setFlag: 所以它是 KVO 兼容的:

-(void)setFlag:(BOOL)flag
{
    if (_flag == flag) {
        return;
    }
    [self willChangeValueForKey:kFooFlagKey];
    _flag = flag;
    [self didChangeValueForKey:kFooFlagKey];
}

在类的初始化程序中,将 self 添加为您要监视的键路径的观察者。在这种情况下,它将用于属性“标志”。

[self addObserver:self
   forKeyPath:kFooFlagKey
      options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
      context:NULL];

不要忘记删除类中的观察者'-dealloc:

[self removeObserver:self forKeyPath:kFooFlagKey];

创建计时器以重复触发(firingCallBack 是每次触发时调用的方法):

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.0f
                                                  target:self
                                                selector:@selector(firingCallBack)
                                                userInfo:nil
                                                 repeats:YES];

实现KVO观察方法:

-(void)observeValueForKeyPath:(NSString *)keyPath
                 ofObject:(id)object
                   change:(NSDictionary *)change
                  context:(void *)context
{
    if ([keyPath isEqualToString:kFooFlagKey]) {
        if (self.flag) {
           [self performSelector:@selector(resetAll) withObject:nil afterDelay:0.0f];
        }
    }
}

实现 -resetAll 随心所欲。当您通过访问器设置标志变量并且标志设置为 YES 时,它将被调用

于 2013-09-04T16:15:33.980 回答
1

您只需启动计时器(已安排)并使其以相对较高的频率重复,并在满足条件时停止。一个计时器可以充当两个计时器,如下所示:

- (void)startTimers {

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}

- (void)timerFired:(NSTimer *)timer {

    if (self.stopTimerA && self.stopTimerB) {
        [timer invalidate];
    } else {
        if (!self.stopTimerA)
            [self timerAFired];
        if (!self.stopTimerB)
            [self timerBFired];
    }
}

- (void)timerAFired {
    // this can be coded like it has it's own timer
    // we didn't pass the timer, so we can't invalidate it
    // to stop...
    self.stopTimerA = YES;
}

- (void)timerBFired {
    // same idea here
}
于 2013-09-04T04:32:47.447 回答
1
Try like this:-

NSTimer *myTimer;
int delay = 6.0;
scanTimer= [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(anotherfunc) userInfo:nil repeats:YES];
myTimer= [NSTimer scheduledTimerWithTimeInterval: delay
                                         target:self
                                       selector: @selector(resetAll:) userInfo:nil
                                        repeats:NO];
[NSApp beginSheet:scanningPanel modalForWindow:[self window]
        modalDelegate:self
       didEndSelector:nil
          contextInfo:self];

- (void)resetAll:(NSTimer *)theTimer
{
    if (flag== YES)
    {
    [NSApp endSheet:scanningPanel];
    [scanningPanel orderOut:self];
        flag=NO;
    }
    else
    {
   delay +=6.0;
myTimer= [NSTimer scheduledTimerWithTimeInterval: delay
                                         target:self
                                       selector: @selector(resetAll:) userInfo:nil
                                        repeats:NO];
    [NSApp beginSheet:scanningPanel modalForWindow:[self window]
        modalDelegate:self
       didEndSelector:nil
          contextInfo:self];
    }
}
于 2013-09-04T05:47:56.790 回答