很久以前就触及了UIButton

时间:2012-10-03 14:27:06

标签: objective-c xcode uibutton

我已经有UIButton每次触摸时都会增加一个变量。 我现在要做的是当我们触及这个UIButton时,变量每0.5秒递增一次。

有什么想法吗?

1 个答案:

答案 0 :(得分: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++;
}