Here's the code I have written that matches your requirements. Make sure to link the plus and minus button outlets and its target events(touch up inside) in your storyboard. If it's not clear just let me know.
#import "ViewController.h"
@interface ViewController () {
NSTimer *speedTimer;
}
//Link this in your storyboard Outlets
@property (weak, nonatomic) IBOutlet UILabel *speedUILabel;
@property (weak, nonatomic) IBOutlet UIButton *plusUIButton;
@property (weak, nonatomic) IBOutlet UIButton *minusUIButton;
//Link this in your storyboard events respectively
- (IBAction)plusUIButton:(id)sender;
- (IBAction)minusUIButton:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UILongPressGestureRecognizer *plusButtonPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
UILongPressGestureRecognizer *minusButtonPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
[_plusUIButton addGestureRecognizer:plusButtonPress];
[_minusUIButton addGestureRecognizer:minusButtonPress];
plusButtonPress.delegate = self;
minusButtonPress.delegate = self;
plusButtonPress.minimumPressDuration = 0.5;
minusButtonPress.minimumPressDuration = 0.5;
plusButtonPress.allowableMovement = 0.5;
minusButtonPress.allowableMovement = 0.5;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)plusUIButton:(id)sender {
NSInteger plus = [_speedUILabel.text integerValue] + 1;
if (plus >= 0) {
_speedUILabel.text = [NSString stringWithFormat:@"%ld", (long)plus];
}
}
- (IBAction)minusUIButton:(id)sender {
NSInteger minus = [_speedUILabel.text integerValue] - 1;
if (minus >= 0) {
_speedUILabel.text = [NSString stringWithFormat:@"%ld", (long)minus];
}
}
- (void) longPressHandler: (UILongPressGestureRecognizer *) gesture {
if (gesture.view == _plusUIButton) {
if(gesture.state == UIGestureRecognizerStateBegan) {
speedTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(plusUIButton:) userInfo:nil repeats:true];
}
else if(gesture.state == UIGestureRecognizerStateEnded) {
[speedTimer invalidate];
speedTimer = nil;
}
}
else if (gesture.view == _minusUIButton) {
if(gesture.state == UIGestureRecognizerStateBegan) {
speedTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(minusUIButton:) userInfo:nil repeats:true];
}
else if(gesture.state == UIGestureRecognizerStateEnded) {
[speedTimer invalidate];
speedTimer = nil;
}
}
}
@end