UIButton长按事件

| 我想模仿长按按钮,该怎么办?我认为需要一个计时器。 我看到
UILongPressGestureRecognizer
,但是我该如何利用呢?     
已邀请:
        您可以通过创建“ 0”实例并将其附加到按钮开始。
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.button addGestureRecognizer:longPress];
[longPress release];
然后实现处理手势的方法
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
         NSLog(@\"Long Press\");
    }
}
现在,这将是基本方法。您还可以设置印刷机的最短持续时间以及允许的错误数量。还要注意的是,如果您在识别出手势之后便多次调用该方法,那么如果您想在其结束时执行某些操作,则必须检查其状态并进行处理。     
        作为可接受答案的替代方法,可以使用Interface Builder在Xcode中非常轻松地完成此操作。 只需将“长按手势识别器”从对象库中拖放到想要长按动作的按钮顶部即可。 接下来,将刚添加的“长按手势识别器”中的“动作”连接到视图控制器,并选择类型为“ 0”的发送者。在“ѭ5”的代码中使用此代码,该代码与公认答案中建议的代码非常相似: 在Objective-C中:
if ( sender.state == UIGestureRecognizerStateEnded ) {
     // Do your stuff here
}
或在Swift中:
if sender.state == .Ended {
    // Do your stuff here
}
但我必须承认,尝试后,我更喜欢@shengbinmeng提出的建议,以作为已接受答案的注释,该建议使用: 在Objective-C中:
if ( sender.state == UIGestureRecognizerStateBegan ) {
     // Do your stuff here
}
或在Swift中:
if sender.state == .Began {
    // Do your stuff here
}
区别在于使用
Ended
,您可以在抬起手指时看到长按的效果。使用
Began
,即使在您将手指抬离屏幕之前,只要系统抓住长按,您就会看到长按的效果。     
        接受答案的Swift版本 我做了额外的修改,使用
UIGestureRecognizerState.Began
而不是
.Ended
,因为这可能是大多数用户自然期望的。尝试一下它们,然后自己看看。
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var button: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        // add gesture recognizer
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
        self.button.addGestureRecognizer(longPress)

    }

    func longPress(gesture: UILongPressGestureRecognizer) {
        if gesture.state == UIGestureRecognizerState.began {
            print(\"Long Press\")
        }
    }

    @IBAction func normalButtonTap(sender: UIButton) {
        print(\"Button tapped\")
    }
}
    
        尝试这个: 在
viewDidLoad:
中添加按钮,如下所示
-(void)viewDidLoad {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setTag:1]; //you can set any integer value as tag number
    btn.title = @\"Press Me\";
    [btn setFrame:CGRectMake(50.0, 50.0, 60.0, 60.0)];

    // now create a long press gesture
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressTap:)];
    [btn addGestureRecognizer:longPress];
}
现在像这样调用手势方法
-(void)longPressTap:(id)sender {
     UIGestureRecognizer *recognizer = (UIGestureRecognizer*) sender
    // Recogniser have all property of button on which you have clicked
    // Now you can compare button\'s tag with recogniser\'s view.tag  
    // View frame for getting the info on which button the click event happened 
    // Then compare tag like this
    if(recognizer.view.tag == 1) { 
       // Put your button\'s click code here
    }

    // And you can also compare the frame of your button with recogniser\'s view
    CGRect btnRect = CGRectMake(50.0, 50.0, 60.0, 60.0);
    if(recogniser.view.frame == btnRect) {
       //put your button\'s click code here
    }

   // Remember frame comparing is alternative method you don\'t need  to write frame comparing code if you are matching the tag number of button 
}
    
        我认为您需要我的解决方案。 您应该单按此代码
- (IBAction)buttonDidPress:(id)sender {
    NSLog(\"buttonDidPress\");
}
首先,向按钮添加长按手势
- (void)viewWillAppear:(BOOL)animated
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonDidLongPress:)];
    [self.button addGestureRecognizer:longPress];
}
然后,如果识别出长按手势,则反复调用单按事件。
- (void)buttonDidLongPress:(UILongPressGestureRecognizer*)gesture
{
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
        {
            self.timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(buttonDidPress:) userInfo:nil repeats:YES];

            NSRunLoop * theRunLoop = [NSRunLoop currentRunLoop];
            [theRunLoop addTimer:self.timer forMode:NSDefaultRunLoopMode];
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            [self.timer invalidate];
            self.timer = nil;
        }
            break;
        default:
            break;
    }
}
    
        对于Swift 4,需要更改\“ func longPress \”以使其起作用:
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var button: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        // add guesture recognizer
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
        self.button.addGestureRecognizer(longPress)

    }

   @objc func longPress(_ guesture: UILongPressGestureRecognizer) {
        if guesture.state == UIGestureRecognizerState.began {
            print(\"Long Press\")
        }
    }

    @IBAction func normalButtonTap(sender: UIButton) {
        print(\"Button tapped\")
    }
}
    
        我的应用程序有一个子类的UIButton,所以我退出了实现。您可以将其添加到子类中,也可以像UIButton类别一样轻松地对其进行重新编码。 我的目标是将长按添加到按钮上,而不用所有代码弄乱我的视图控制器。我已经决定在手势识别器状态开始时应调用该动作。 出现警告,我从来没有想过要解决。说这是可能的泄漏,以为我已经测试了代码并且没有泄漏。
@interface MYLongButton ()
@property (nonatomic, strong) UILongPressGestureRecognizer *gestureRecognizer;
@property (nonatomic, strong) id gestureRecognizerTarget;
@property (nonatomic, assign) SEL gestureRecognizerSelector;
@end

@implementation MYLongButton

- (void)addLongPressTarget:(CGFloat)interval target:(id)target action:(SEL)selector
{
    _gestureRecognizerTarget = target;
    _gestureRecognizerSelector = selector;
    _gestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestureRecognizer:)];
    _gestureRecognizer.minimumPressDuration = interval;

    [self addGestureRecognizer:_gestureRecognizer];
}

- (void)handleLongPressGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        NSAssert([_gestureRecognizerTarget respondsToSelector:_gestureRecognizerSelector], @\"target does not respond to selector\");

        self.highlighted = NO;

        // warning on possible leak -- can anybody fix it?
        [_gestureRecognizerTarget performSelector:_gestureRecognizerSelector withObject:self];
    }
}
要分配动作,请将此行添加到viewDidLoad方法。
[_myLongButton addLongPressTarget:0.75 target:self selector:@selector(longPressAction:)];
应该像所有IBAction(没有IBAction)一样定义该动作。
- (void)longPressAction:(id)sender {
    // sender is the button
}
    
        没有任何效果,因此我尝试在
IBAction
中编写longpress代码或在
Controller
中从
storyboard
中单击按钮,而不是在
viewDidLoad
中编写
- (IBAction)btnClick:(id)sender {

    tag = (int)((UIButton *)sender).tag;

// Long press here instead of in viewDidLoad

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPress.cancelsTouchesInView = NO;
    [sender addGestureRecognizer:longPress];

}
    

要回复问题请先登录注册