我已经用来自另一个堆栈溢出线程( UIButton Touch and Hold )的代码在按钮(UIButton) hold上实现了一个重复的任务。
由于各种原因,我希望再次延期。definition of repeat delay
然而,我不能完全理解如何做到这一点。对于iPhone的编码,我还是个新手。
发布于 2012-10-19 07:02:11
灵感来自于你建议的代码,你可以这样做:
创建一个在按下按钮时启动的NSTimer,并每隔x秒触发一次方法。
报头(.h):
// Declare the timer and the needed IBActions in interface.
@interface className {
NSTimer * timer;
}
-(IBAction)theTouchDown(id)sender;
-(IBAction)theTouchUpInside(id)sender;
-(IBAction)theTouchUpOutside(id)sender;
// Give the timer properties.
@property (nonatomic, retain) NSTimer * timer;实现文件(.m):
// Synthesize the timer
// ... 为"Touch Down“创建一个IBAction,以启动计时器,该计时器将每隔2秒触发一次操作方法。然后为"Touch Up Inside“和"Touch Up Outside”创建另一个IBAction,以使计时器无效。
例如:
-(IBAction)theTouchDown {
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(action:)
userInfo:nil
repeats:NO];
}
-(IBAction)theTouchUpInside {
[self.timer invalidate];
self.timer = nil;
}
-(IBAction)theTouchUpOutside {
[self.timer invalidate];
self.timer = nil;
}然后,在由NSTimer触发的方法中,执行所需的任何操作:
-(void)action:(id)sender {
// ...
}https://stackoverflow.com/questions/12964867
复制相似问题