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

我必须使用文本字段输入昵称,还必须点击UIView才能/禁用男性/女性性别状态。

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

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 = ""
    }
}

这是屏幕截图。

最佳答案

我认为使用透明按钮而不是在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的宽度等于视图的宽度,并用于点击。
  • 关于ios - 无法检测到ScrollView中的Swift UI组件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47193499/

    10-10 03:53