我必須爲標籤和ImageView添加tapGesture。如何爲標籤和ImageView創建通用的輕擊手勢方法?如何使用swift編寫UITapGestureRecognizer的通用方法
//TapGestureHandler
extension EditViewController : UIGestureRecognizerDelegate
{
//Add Gesture on ImageView
func addGesture()
{
//Gesture Male
let maleTapGesture = UITapGestureRecognizer(target: self, action: #selector(maleGestureTap))
maleTapGesture.delegate = self
imgViewMale.addGestureRecognizer(maleTapGesture)
//GestureFemale
let FemaleTapGesture = UITapGestureRecognizer(target: self, action: #selector(femaleGestureTap))
FemaleTapGesture.delegate = self
imgViewFemale.addGestureRecognizer(FemaleTapGesture)
//Gesture MaleFemale
let maleFeTapGesture = UITapGestureRecognizer(target: self, action: #selector(maleFeGestureTap))
maleFeTapGesture.delegate = self
imgViewMaleFe.addGestureRecognizer(maleFeTapGesture)
}
//Tap Gesture Male
func maleGestureTap()
{
imgViewMale.backgroundColor = hexStringToUIColor(hex: "#CBFFE2")
}
//Tap Gesture Female
func femaleGestureTap()
{
imgViewFemale.backgroundColor = hexStringToUIColor(hex: "#CBFFE2")
}
//Tap Gesture MaleFemale
func maleFeGestureTap()
{
imgViewMaleFe.backgroundColor = hexStringToUIColor(hex: "#CBFFE2")
}
}
我不知道我們該怎麼寫一個通用的方法。
你們爲什麼不創建它爲UIView? UILabel和UIImageView擴展UIView –
如何爲UIView創建? 謝謝 – kishor0011
請檢查我更新的問題。 – kishor0011