我有自定義視圖,應該點擊並執行一些操作。 我在同一屏幕上有兩個CustomView。我想要檢測哪個被點擊做不同的動作。檢測哪個uview被觸摸
是否可以在那裏設置一些ID來檢測哪個被準確點擊?
這裏是我的CustomView
protocol CostomViewDelegate: class {
func viewClicked()
}
class CostomView: UIView, UIGestureRecognizer {
@IBOutlet weak var placeholderlbl: UILabel!
@IBOutlet weak var textLbl: UILabel!
weak var delegate: CostomViewDelegate?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.layer.backgroundColor = UIColor.red.cgColor
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.layer.backgroundColor = UIColor.white.cgColor
delegate?.viewClicked()
}
}
非常感謝您的回答,它真的幫助了我。我認爲'現代'的方式是使用代表團,你可以給我一個'hold closure和invoke closure'的例子鏈接嗎?我真的沒有明白這是什麼意思。 – pmb