长按多次调用 UIButton 的动作

Posted

技术标签:

【中文标题】长按多次调用 UIButton 的动作【英文标题】:Call UIButton's action multiple times on long press 【发布时间】:2017-01-23 13:42:15 【问题描述】:

我有2个UIButtons,每个都改变中间的数字(请看图)。

我希望当用户按住按钮时,数字仍然会发生变化(现在,当您想从 100 更改为 200 时,您需要点击 100 次,这并不好,我希望当您按住按钮时数字会发生变化),这可能意味着每个UIButon 的操作都应该被一次又一次地调用,直到用户释放按钮。

我该怎么做?谢谢!

【问题讨论】:

你应该看看这个***.com/a/34236125/4918968 为什么不触发基于定时器的调用,当触碰并在触碰时使定时器失效 这个链接只显示了那个东西 @PatelJigar 这解决了问题。谢谢! 为什么不使用点击手势而不是按钮? 【参考方案1】:

这是我编写的符合您要求的代码。确保在故事板中链接加号和减号按钮插座及其目标事件(内部修饰)。如果不清楚,请告诉我。

#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

【讨论】:

【参考方案2】:

您必须使用 UILongPressGestureRecognizer,这是一个连续调用动作,而不是通过按钮点击调用方法。因此,只需将左/右按钮放在单独的视图中,并将 UILongPressGestureRecognizer 添加到这些左/右视图并将目标添加到这些识别器,例如

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

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

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

希望这能解决您的问题。

【讨论】:

【参考方案3】:

在两个 UIButton 上使用 UILongPressGestureRecognizer

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

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

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

更新屏幕的状态更改代码。

- (void) longPressHandler: (UILongPressGestureRecognizer *) gesture 

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



【讨论】:

【参考方案4】:

你可以在 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")
    

【讨论】:

对不起,我之前的帖子不正确。现在,代码应该可以工作了。

以上是关于长按多次调用 UIButton 的动作的主要内容,如果未能解决你的问题,请参考以下文章

UIButton 动作

UIbutton 长按和 Touch Up 里面

iOS:UIButton touch up inside 不响应多次按下

iOS:如果您长按然后释放它,UIButton 文本不会立即更改

3d 触摸快速动作调用 UIButton

具有按住动作和释放动作的 UIButton