在Swift中单击按钮而不释放触摸

时间:2018-06-03 18:01:31

标签: ios swift xcode uibutton

我连续有5个按钮:

enter image description here

我试图在不释放触摸的情况下检测所有按钮上的用户触摸 - 就像触摸钢琴一样。

到目前为止,我试过这个:

func buildButtons() {
      button.addTarget(self, action: #selector(buttonAction), for: .touchDown)
      view.addSubview(button)
}

@objc func buttonAction(sender: UIButton) {
      sender.isEnabled = false
      sender.isEnabled = true
      print(sender.tag)
}

但我得到的是一个被触摸的按钮然后当我触摸下一个按钮而没有释放时,没有任何作用。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

我认为这是一个与多点触控有关的问题。您可以尝试启用视图的多点触控。

self.view.isMultipleTouchEnabled = true

答案 1 :(得分:0)

我不确定究竟什么不适合你,我已经实现了一个简单的测试项目,并且在没有发布第一个按钮的情况下触摸下一个按钮。这是我写的代码

     override func viewDidLoad() {
        super.viewDidLoad()

        let stackView = UIStackView(frame: CGRect(origin: CGPoint(x: 40.0, y: 40.0), size: CGSize(width: 300, height: 45)))
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.spacing = 10

        let button1 = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: 45, height: 45)))
        let button2 = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: 45, height: 45)))
        let button3 = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: 45, height: 45)))

        [button1, button2, button3].forEach { button in
            button.backgroundColor = .blue
            stackView.addArrangedSubview(button)
            button.addTarget(self, action: #selector(touchDownAction), for: .touchDown)
        }

        view.addSubview(stackView)
    }

    @objc func touchDownAction() {
        print("touched button")
    }

此代码在运行ios 11.4的iphone上测试过。你能提供一些额外的信息吗?

编辑:我已经更新了解决方案,以便在将手指从一个按钮拖到下一个按钮时起作用,对不起我的误解。希望这对你有用。另外请记住,现在你还有另一个问题,当你的手指在按钮内时,你的触地动作会被连续调用。

    var button1: UIButton!
    var button2: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        let stackView = UIStackView(frame: CGRect(origin: CGPoint(x: 40.0, y: 40.0), size: CGSize(width: 300, height: 45)))
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.spacing = 10

        button1 = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: 45, height: 45)))
        button2 = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: 45, height: 45)))

        [button1, button2].forEach { button in
            button!.backgroundColor = .blue
            stackView.addArrangedSubview(button!)
        }

        view.addSubview(stackView)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touchLocation = touches.first!.location(in: view)
        let button1FrameInMainView = button1.convert(button1.bounds, to: view)
        let button2FrameInMainView = button2.convert(button2.bounds, to: view)

        if button1FrameInMainView.contains(touchLocation) {
            print("touch inside button 1")
        }

        if button2FrameInMainView.contains(touchLocation) {
            print("touch inside button 2")
        }
    }