简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM