我如何设置一个按钮(附有 IBAction 和 UIButton)以在按下按钮时继续运行 IBAction 或函数,持续运行一个函数直到按钮松开。
我应该附加一个值(value)改变的接收器吗?
简单的问题,但我找不到答案。
最佳答案
[myButton addTarget:self action:@selector(buttonIsDown) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:@selector(buttonWasReleased) forControlEvents:UIControlEventTouchUpInside];
- (void)buttonIsDown
{
//myTimer should be declared in your header file so it can be used in both of these actions.
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(myRepeatingAction) userInfo:nil repeats:YES];
}
- (void)buttonWasReleased
{
[myTimer invalidate];
myTimer = nil;
}
关于iphone - 按下按钮时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11998361/