繁体   English   中英

如何在iOS中一起检测两个按钮长按手势?

[英]How to detect two buttons long press gesture together in iOS?

我的应用程序中有两个按钮,当两个按钮同时按住至少3秒钟时,我想检测手势。 我知道可以检测到单个按钮的长按手势,但是当两个按钮同时按下并按住几秒钟时,我想调用一个功能。 反正有做吗?

您可以使用此代码。

@IBAction func longPress(gesture:UILongPressGestureRecognizer) {
    let view = gesture.view!
    print("state = \(gesture.state.rawValue)")
    if gesture.state != UIGestureRecognizerState.Ended && gesture.state != UIGestureRecognizerState.Cancelled {
        view.tag = 1
    } else {
        view.tag = 0
    }
    if button1.tag == 1 && button2.tag == 1 {
        print("pressed both buttons for 3 seconds")
    } else {
        print("not pressed both buttons for 3 seconds")
    }
}

想法是保留对两个按钮的引用,然后在那些连接到单个动作的按钮上使用长手势识别器。 在动作内部,您将获得进行手势的按钮,然后将其标记更改为1。如果手势结束或被取消,则将其设置为0。同时按下两个按钮时,两个按钮的标记都将为1。在这种情况下,您将知道按钮已按下3秒钟以上。 请注意,您必须在情节提要中设置最短持续时间,并保持按钮的出线位置。

如果需要,可以使用其他条件代替标记来检查长按状态,但是可以使用相同的想法。

在ObjC

-(IBAction)longPress:(UILongPressGestureRecognizer*) gesture {
    UIButton* button = (UIButton*)gesture.view;
    if (gesture.state != UIGestureRecognizerStateEnded && gesture.state != UIGestureRecognizerStateCancelled)
    {
        button.tag = 1;
    } else {
        button.tag = 0;
    }
    if (button1.tag == 1 && button2.tag == 1) {
        NSLog(@"pressed both buttons for 3 seconds");
    } else {
        NSLog(@"not pressed both buttons for 3 seconds");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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