When UIButton is held, increment by pre-set int value
I am working for 开发者_开发问答a client that wants his button - when pressed - to add a preset value to a value in a text field, but when held to increment while held by the same amount. I've been looking all afternoon and I can't find a solid answer on this.
I am working off his code and his increment code is as follows:
-(IBAction)incrementInput1:(id)sender{
inputValue1.text=[NSString stringWithFormat:@"%g",[[inputValue1 text] floatValue]+inc1];
note.text=@" ";
[self calcValue];
}
This is for each button (of which there are four).
Thank you so much for reading and extra thanks if you help out :-D
I think this will help you.
-(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;
}
initiate a time in action triggered by touchedDownInside, which will increment your value and repeat.
Then, stop the timer in action triggered when touches ended.
精彩评论