无法检测到ScrollView中的Swift UI组件

时间:2017-11-09 03:36:43

标签: ios swift uiview scrollview

我在ScrollView中有一个UIView。但是这个UIVIew中的每个组件甚至本身都无法检测到手势。

我必须使用textfield输入昵称,并且还必须点击UIView才能启用/禁用男/女性别状态。

这是我的代码。即使我尝试过在互联网上找到的其他解决方案,它仍然无法正常工作。

let tapViewMale = UITapGestureRecognizer()
    tapViewMale.addTarget(self, action:#selector(viewMaleClicked))
    self.viewMain!.addGestureRecognizer(tapViewMale)
    let tapViewFemale = UITapGestureRecognizer()#imageLiteral(resourceName: "Screen Shot 2017-11-09 at 12.15.55 PM.png")
    tapViewFemale.addTarget(self, action:#selector(viewFemaleClicked(_:)))
    self.checkboxFemale.isUserInteractionEnabled = true
    self.checkboxFemale.addGestureRecognizer(tapViewFemale)
}

@objc func viewMaleClicked(tapGestureRecognizer: UITapGestureRecognizer) {
    print ("viewMaleClicked")
    if gender == "" || gender == "F" {
        print ("1")
        checkboxMale.image = UIImage(named: "checked_checkbox")
        checkboxFemale.image = UIImage(named: "unchecked_checkbox")
        gender = "M"
    } else {
        print ("2")
        checkboxMale.image = UIImage(named: "unchecked_checkbox")
        gender = ""
    }
}

@objc func viewFemaleClicked(_ sender:AnyObject) {
    print ("viewFemaleClicked")
    if gender == "" || gender == "M" {
        print ("1")
        checkboxFemale.image = UIImage(named: "checked_checkbox")
        checkboxMale.image = UIImage(named: "unchecked_checkbox")
        gender = "F"
    } else {
        print ("2")
        checkboxFemale.image = UIImage(named: "unchecked_checkbox")
        gender = ""
    }
}

以下是截图。

full component tree

1 个答案:

答案 0 :(得分:0)

我认为使用透明按钮的简单方法,而不是在scrollView内部向uiview提供手势。

@IBAction func onClickBtnMale(_ sender: Any)
    { if gender == "" || gender == "F" {
        print ("1")
        checkboxMale.image = UIImage(named: "checked_checkbox")
        checkboxFemale.image = UIImage(named: "unchecked_checkbox")
        gender = "M"
    } else {
        print ("2")
        checkboxMale.image = UIImage(named: "unchecked_checkbox")
        gender = ""
    }
}
  • 您可以在视图中使用相同宽度的uibutton并将其用于点击。