0

I have 2 UIButtons that each one changes the number in the middle (please see the picture).

I want that when the user holds the button the numbers will still be changing (now, when you want to change from 100 to 200 you need to tap 100 times and it's not good, I want that when you hold the button the numbers will be changing), that probably means that each UIButon's action should be called again and again until the user releases the button.

Photo

How can I do that? Thanks!

FS.O6
  • 1,394
  • 2
  • 20
  • 42

4 Answers4

3

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
0

You can do like this in Swift:

@IBOutlet weak var btn: UIButton!

override func viewDidLoad() {

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.Long))  //Tap function will call when user tap on button
    let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.Long)) //Long function will call when user long press on button.

    tapGesture.locationInView(btn);

    btn.addGestureRecognizer(tapGesture)   // To set the tap gesture recognizer: the function Tap () will be calling when the user select the button normally.
    btn.addGestureRecognizer(longGesture)   // To set the long gesture recognizer: the function Long () will be calling when the user select the button with a long press.
}

func Tap() {

    print("Tap happend")
    }

func Long() {

    print("Long press")
    }
SphynxTech
  • 1,799
  • 2
  • 18
  • 38
0

You have to use the UILongPressGestureRecognizer which is a continuous calling action instead of calling methods with button tap. So just put the left/Right buttons in separate views and add UILongPressGestureRecognizer to these left/Right views and add target to these recognizers e.g

  UILongPressGestureRecognizer *leftLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(decreaseNum:)];

  UILongPressGestureRecognizer *rightLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(increaseNum:)];

  [leftView addGestureRecognizer:leftLongPress];
  [rightView addGestureRecognizer:rightLongPress];

Hope this will resolve your problem .

Nisar Ahmad
  • 885
  • 1
  • 10
  • 25
0

Use UILongPressGestureRecognizer on both UIButton

UILongPressGestureRecognizer *LeftButtonPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];

  UILongPressGestureRecognizer *RightButtonPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];

  [btnLeft addGestureRecognizer:LeftButtonPress];
  [btnRight addGestureRecognizer:RightButtonPress];

code on state change for update screen.

- (void) longPressHandler: (UILongPressGestureRecognizer *) gesture {

    if(gesture.state == UIGestureRecognizerStateChanged) {
// update number on screen

}
}
Hasham
  • 140
  • 8