[英]When UIButton is held, increment by pre-set int value
我正在为一个希望他的按钮(按下时)将预设值添加到文本字段中的值的客户端工作,但是当按住该按钮以相同数量递增时。 我一直在找一个下午,却找不到可靠的答案。
我正在研究他的代码,他的增量代码如下:
-(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;
}
触发touchedDownInside触发的动作时间,这将增加您的值并重复执行。
然后,停止触摸结束后触发的活动计时器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.