【问题标题】:Detect two or more UIButtons being pressed at the same time检测同时按下两个或多个 UIButton
【发布时间】:2016-09-01 23:55:32
【问题描述】:

故事板上有三个UIButtons

什么是最直接的检测时间的方法:

  • 同时按下两个UIButtons

  • 同时按下三个UIButtons

【问题讨论】:

  • 不知道是否有意义,我觉得它很有趣..
  • 如果按钮被突出显示,它当前被按下例如if button.highlighted && button2.highlighted
  • 并且您可以检查与任一按钮连接的函数内的代码,因此当单击一个按钮时,它会检查是否也单击了其他按钮
  • 使用 3 次触摸的 UITapGestureRecognizer 实例然后在委托中使用可能是正确的方法 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch: check touches location with CGRectContainsPoint..

标签: ios swift uibutton storyboard uitouch


【解决方案1】:

我认为如果你使用 Touch Down Event 和 Touch Up Event 会很容易。

触地: 手指按下时

修饰: 当手指抬起时。

因此,在 Touch Down 设置该特定按钮被触摸的变量,并在 Touch Up 事件中检查两个按钮当前是否都被触摸。像这样:

class ViewController: UIViewController {
    var Button_1_Pressed: Int = 0;
    var Button_2_Pressed: Int = 0;

    @IBAction func Button_1_Down(sender: AnyObject) {
        Button_1_Pressed = 1;
    }

    @IBAction func Button_2_Down(sender: AnyObject) {
        Button_2_Pressed = 1;
    }

    @IBAction func Button_1_Up(sender: AnyObject) {
        if (Button_1_Pressed == 1 && Button_2_Pressed == 1){
            println("1- Both Pressed at same time");
        }
        Button_1_Pressed = 0;
    }

    @IBAction func Button_2_Up(sender: AnyObject) {
        if (Button_1_Pressed == 1 && Button_2_Pressed == 1){
            println("2- Both Pressed at same time");
        }
        Button_2_Pressed = 0;
    }
}

您可以使用 XCode 界面构建器设置这些 @IBAction,方法是在按钮上按 CTRL 键,然后选择事件并拖动到您的 swift 文件中。上面的代码只实现了 2 个按钮,但它会让您对其余按钮有所了解。

如果您使用的是 iOS 模拟器,您可以使用 Option 键检测两次触摸。

【讨论】:

  • 谢谢。这给了我这个想法。
猜你喜欢
  • 1970-01-01
  • 2012-01-22
  • 2019-05-21
  • 2014-06-26
  • 2010-12-20
  • 2017-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多