最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Swift中的UIGestureRecognizer中无法避免UIButton触发点击事件

Swift crifan 3937浏览 0评论
背景是:
主的view中已经实现了UIGestureRecognizer
然后在:
UIGestureRecognizerDelegate
中的:
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
已经通过:
CGRectContainsPoint(searchButton.frame, touchPoint)
判断到:
点击到是一个button了:
        let searchImage = UIImage(named:"search_24x24.png")
        let searchFrame = CGRectMake(
            addButton.frame.origin.x - 44,
            0,
            44.0,
            NAVI_BAR_HEIGHT)
        searchButton = UIButton()
        searchButton.frame = searchFrame
        searchButton.addTarget(self, action:"jumpToSearch:", forControlEvents:UIControlEvents.TouchUpInside)
        searchButton.setImage(searchImage,forState:UIControlState.Normal)
        tabbgView.addSubview(searchButton)

    //jump to search view
    func jumpToSearch(button : UIButton){
        print("jumpToSearch")
    }
但是:
不论shouldReceiveTouch中返回是true还是false,结果UIButton的action都会被调用。。。
搜:
swift UIGestureRecognizer UIButton
参考:
后来看到官网中的:
说是:
iOS 6之后,默认的控件的动作,会覆盖掉tap的手势
比如:
button的话,支持单击tap
如果父视图有个手势识别器,结果用户单击tap的话,是按钮,而不是手势识别器,收到对应的tap。
类似的其它控件还有:
  • A single finger single tap on a UIButton, UISwitch, UIStepper, UISegmentedControl, and UIPageControl.
  • A single finger swipe on the knob of a UISlider, in a direction parallel to the slider.
  • A single finger pan gesture on the knob of a UISwitch, in a direction parallel to the switch.
最后,找了个,workaround,临时的,规避的办法解决此问题的:
在此处,点击加号,显示出下拉列表的时候,
(同时会给当前parent view添加tapRecognizer的)
主动去把那个button去disable
    //toggle add drop down list
    func toggleAddDropdownList(){
        addDropdownListView.hidden = !addDropdownListView.hidden

        if !addDropdownListView.hidden {
            self.view.addGestureRecognizer(tapRecognizer)
            searchButton.enabled = false
        }else {
            self.view.removeGestureRecognizer(tapRecognizer)
            searchButton.enabled = true
        }

        //overlayView.hidden = addDropdownListView.hidden

        print("addDropdownListView.hidden=\(addDropdownListView.hidden)")
    }
-》这样,即使在
shouldReceiveTouch
中,返回了啥,结果都无法去调用,此时已经被disable的button(的action)了。

转载请注明:在路上 » [已解决]Swift中的UIGestureRecognizer中无法避免UIButton触发点击事件

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.163 seconds, using 22.01MB memory