ios - 检测同时按下两个或多个 UIButton

标签 ios swift uibutton storyboard uitouch

我在 Storyboard上有三个 UIButtons

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

  • 同时按下了两个 UIButtons

  • 同时按下了三个 UIButtons

最佳答案

我认为如果使用 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 键检测两次触摸。

关于ios - 检测同时按下两个或多个 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37080338/

相关文章:

ios - Swift2 中的 Firebase 身份验证匿名

ios - 更改 UISplitViewController 中 Master 的宽度

ios - 从 3.5 英寸更改为全尺寸应用

ios - 切换方向时从堆栈 View 中删除的元素

ios - titleLable.text 不工作

ios - UIButton - alloc initWithFrame : vs. buttonWithType:

objective-c - 在 Safari 中连接 Wifi 后返回应用程序

ios - UIButton 以相等的宽度快速延伸超过 UIView 的边缘

ios - 如何从 UIScrollView 获取 UIButton 位置

arrays - 快速限制数组大小