UIButton按住 - 重复动作直到松开

dok*_*un1 9 uibutton ios

我希望有一个特定的方法运行并重复自己,只要在按钮上按下某个人的手指.当手指不在按钮上时,我希望该方法停止重复

有没有办法检查在方法实现过程中是否仍然发生touchDown?救命!

bob*_*ble 13

您可以使用UIControlEventTouchDown控制事件来启动方法运行,UIControlEventTouchUpInside或类似地,以检测何时不再"按下"按钮.

设置按钮的操作,例如:

[myButton addTarget:self action:@selector(startButtonTouch:) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:@selector(endButtonTouch:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
Run Code Online (Sandbox Code Playgroud)

(注意上面的内容会导致按钮内外触摸调用endButtonTouch:方法.)

然后添加startButtonTouch:endButtonTouch方法,例如:

- (void)startButtonTouch:(id)sender {
    // start the process running...
}

- (void)endButtonTouch:(id)sender {
// stop the running process...
}
Run Code Online (Sandbox Code Playgroud)