0

我已经有一个UIButton每次被触摸时都会增加一个变量的变量。我现在想做的是,当我们保持接触 thisUIButton时,变量每 0.5 秒递增一次。

有任何想法吗?

4

1 回答 1

1

UIControlEventTouchDown向您启动计时器的事件添加操作。打开UIControlEventTouchUpOutsideUIControlEventTouchUpInside停止计时器。

[button addTarget:self action:@selector(touchedButton:) 
 forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(releasedButton:) 
 forControlEvents:UIControlEventTouchUpOutside | UIControlEventTouchUpInside];

- (void)touchedButton:(id)sender {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self 
                  selector:@selector(timerFired:) userInfo:nil repeats:YES];
}

- (void)releasedButton:(id)sender {
    [self.timer invalidate];
}

- (void)timerFired:(NSTimer *)timer {
    counter++;
}
于 2012-10-03T14:35:55.070 回答