【问题标题】:Swift detect touch in both left and right side of screen simultaneously快速同时检测屏幕左右两侧的触摸
【发布时间】:2017-02-28 22:07:44
【问题描述】:

我已经能够检测到用户是在触摸屏幕的左侧还是右侧,然后执行一项功能。我正在尝试检测屏幕的两侧是否同时被触摸,如果是,则执行另一个功能。

当同时触摸屏幕的两侧时,这就是我所拥有的。

这是在更新功能中。

if (touched) {
        var isRight : Bool = false
        var isLeft : Bool = false

        if(location.x < 0){
        isLeft = true
        moveLeft()
        }
        else if(location.x > 0){
        isRight = true
        moveRight()


            }
        else if (isRight && isLeft){
            moveUp()


        }

    }

【问题讨论】:

  • 为我?这是一个非常令人困惑的触摸动作。 (我猜对于非开发人员来说情况更糟。)你能解释一下你想要检测这个的原因吗?就我个人而言,我会选择另一个手势。但无论如何,也许您可​​以使用捏合手势来检测用户手上每个数字的位置? (是的,我认为这是一个非常糟糕的设计姿态。)
  • 基本上触摸屏幕左侧movesLeft...触摸屏幕右侧movesRight。触摸左右然后发射物体。
  • 所以这是游戏风格?我的两个建议:(1)就像我说的,尝试捏合手势(或者更深入地使用 touchesBegan 和 touchesEnded - 以及检测两次触摸)。 (2) 在每一侧放置某种“层”。 @Yitschak 有一个可能有帮助的答案。

标签: ios swift touches


【解决方案1】:

以下检查所有触摸侧的触摸并将联合值构建为整数值,然后可以使用 switch 语句进行检查:

struct Sides : OptionSet {
    let rawValue: Int

    static let left = Sides(rawValue:1)
    static let right = Sides(rawValue:2)

    static let both : Sides = [.left, .right]
    static let none : Sides = []
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    // Get sides touched
    let touched = touches.map { return $0.location(in: view) }.reduce(Sides.none) {
        if $1.x < 0 {
            return [ $0, Sides.left ]
        }
        else if $1.x > 0 {
            return [ $0, Sides.right ]
        }
        else {
            return $0
        }
    }

    switch(touched) {
    case Sides.left:
        // handle left
        break

    case Sides.right:
        // handle right
        break

    case Sides.both:
        // handle both
        break

    case Sides.none:
        fallthrough

    default:
        // none
        break

    }
}

【讨论】:

  • 我认为Sides 可能会更好地实现为OptionSetdeveloper.apple.com/reference/swift/optionset
  • 这只是返回“Right”?
  • 真的@BallpointBen,我当时想不出OptionSet :)
  • @AlexIngram 这可能是因为我复制了您的左/右检测代码,它将位置与零进行比较。
  • 与大卫相比应该是什么?我试过 > self.frame.midX 但没有运气
【解决方案2】:

使用该函数获取ant的触摸位置数组,测试它们的位置是否为(左右)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if touches.count == 2 {
        // 2 touches so we got 2 positions
        let tch_1 = touches[touches.startIndex].location(in: self.view)
        let tch_2 = touches[touches.endIndex].location(in: self.view)
        if (tch_1.x < self.view.frame.size.width / 2 && tch_2.x >= self.view.frame.size.width / 2) || (tch_2.x < self.view.frame.size.width / 2 && tch_1.x >= self.view.frame.size.width / 2) {
            //touch detected in both left and right side of screen simultaneously

        }
    }
}

【讨论】:

    【解决方案3】:

    我没有时间编写代码(我现在正在打电话 - 如果您发布所有功能会有所帮助)。基本上你应该得到一个触摸数组作为参数。您比遍历它们并打开标志:触摸右侧/左侧。然后你根据标志调用你的函数

    【讨论】:

    • 该函数只是将一个精灵节点从播放器发送到屏幕顶部。所以就像向上发射子弹一样。除了在计时器上自动射击之外,我想不出任何其他的开火方式。我认为接触双方将是这样做的好方法
    • 我不明白为什么-1。我写了一个很好的答案,(2年前!)只是没有编写代码,但正如我所说,他得到了一个触摸数组作为参数,这就是解决方案的关键。迭代它们,并了解他是只剩下,只有右边还是两者兼而有之!
    猜你喜欢
    • 2023-04-05
    • 2011-05-15
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多