UIButton不可点击但接收手势

时间:2018-04-04 13:53:40

标签: ios uibutton uipangesturerecognizer

我有一个UIButton,我希望能够使用pan gesture移动,但无法点击它。我想要的是当用户点击按钮按钮时不改变其外观,就像点击视图一样。将isEnabled设置为false并将userInteractionIsEnabled设置为false会导致按钮未收到pan events。我可以使用自定义视图实现所需的效果,但是我可以使用按钮以某种方式执行此操作。 发布按要求创建按钮的代码段:

func createAnswerButton() {
    let width : CGFloat = 154
    let height : CGFloat = 60
    let originX = (view.frame.size.width - width) / 2
    let originY = startOverButton.frame.origin.y + collectionView.frame.size.height
    let frame = CGRect(x: originX, y: originY, width: width, height: height)
    let button = UIButton(frame: frame)
    answerButton = button
    button.backgroundColor = .clear
    self.answerButton.setTitleColor(.white, for: .normal)
    let image = UIImage(named: "item_neutral")
    button.setBackgroundImage(image, for: .normal)
    view.addSubview(button)

    let panGesture = UIPanGestureRecognizer(target: self, action: (#selector(MoviesViewController.didPanButton(_:))))
    button.addGestureRecognizer(panGesture)
}

3 个答案:

答案 0 :(得分:1)

要实现此目的,您必须将按钮的adjustsImageWhenHighlighted设置为false(如果您想以编程方式执行此操作)。在界面构建器中,您可以将按钮type更改为custom

以下是以编程方式执行此操作的代码:

 override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    let width : CGFloat = 154
    let height : CGFloat = 60
    let originX = (view.frame.size.width - width) / 2
    let originY = (view.frame.size.height - width) / 2
    let frame = CGRect(x: originX, y: originY, width: width, height: height)

    let button = UIButton(type: .custom)
    button.adjustsImageWhenHighlighted = false
    button.frame = frame

    button.addTarget(self, action: #selector(buttonInCodeTapped), for: .touchUpInside)
    button.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(panForButtonInCode(recognizer:))))
    button.setBackgroundImage(UIImage(named: "X"), for: .normal)
    view.addSubview(button)
}

以下是该属性设置为false时的结果:   enter image description here

以下是该属性设置为true时的结果(您可以看到点按X时突出显示):

enter image description here

答案 1 :(得分:0)

几乎你必须说按钮更喜欢一个手势而不是另一个手势

例如:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, 
      shouldRequireFailureOf otherGestureRecognizer:  UIGestureRecognizer) -> Bool {
    // Don't recognize a single tap until a double-tap fails.
     if gestureRecognizer == self.tapGesture && 
      otherGestureRecognizer == self.doubleTapGesture {
  return true

}       返回false }

有关更多信息,请访问以下链接:

https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/coordinating_multiple_gesture_recognizers/preferring_one_gesture_over_another

答案 2 :(得分:0)

试试这个,

func createAnswerButton() {
        let width : CGFloat = 154
        let height : CGFloat = 60
        let originX = (view.frame.size.width - width) / 2
        let originY = startOverButton.frame.origin.y + collectionView.frame.size.height
        let frame = CGRect(x: originX, y: originY, width: width, height: height)
        let button = UIButton(frame: frame)
        answerButton = button
        button.backgroundColor = .clear
        self.answerButton.setTitleColor(.white, for: .normal)
        let image = UIImage(named: "item_neutral")
        button.setBackgroundImage(image, for: .normal)
        button.addTarget(self, action: #selector(self.buttonClicked(_:)), for: .touchUpInside)
        view.addSubview(button)

        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture))
        button.addGestureRecognizer(panGesture)
    }
    @objc func handlePanGesture(sender: UIPanGestureRecognizer? = nil) {
        print("Pan Gesture detected")
        var buttonCenter : CGPoint = button.center
        if sender?.state == .began {
            buttonCenter = button.center
        } else if sender?.state == .ended || sender?.state == .failed || sender?.state == .cancelled {
            button.center = buttonCenter
        } else {
            let location = sender?.location(in: view)
            button.center = location!
        }
        print("\(buttonCenter)")
    }
    @objc func buttonClicked(_ sender: UIButton){
        print("Button Click detected")
    }