我正在为一位客户工作,他希望他的按钮在按下时将预设值添加到文本字段中的值,但在按住时增加相同的数量。我整个下午都在寻找这个问题,但找不到可靠的答案。
我正在处理他的代码,他的增量代码如下:
-(IBAction)incrementInput1:(id)sender{
inputValue1.text=[NSString stringWithFormat:@"%g",[[inputValue1 text] floatValue]+inc1];
note.text=@" ";
[self calcValue];
}
这适用于每个按钮(其中有四个)。
非常感谢您的阅读,如果您能提供帮助,请额外感谢:-D
最佳答案
我想这会对你有帮助。
-(void)incrementValue:(id)sender {
<Code to increment value>
}
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *array = [touches allObjects];
UITouch *specificTouch = [array objectAtIndex:0];
currentTouch = [specificTouch locationInView:yourUIbuttonName];
if (CGRectContainsPoint(yourUIbuttonName.bounds, currentTouch)) {
timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(incrementValue:) repeats:YES];
}
}
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
if (timer != nil)
[timer invalidate];
timer = nil;
}
关于iphone - 当UIButton被按住时,按预设的int值递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5616996/