我有一個UITableView
用的UITableViewCell這樣的:輕按通過UIScrollView的疊加在一個UITableViewCell一個UIButton不會觸發按鈕的目標
我添加了一個UITapGestureRecognizer
(我們稱之爲目標targetCell
)上的當用戶點擊一個單元格時管理tableView。在Button
上,我們稱目標爲targetButton
。
在單元格頂部,我添加了一個UIScrollView
作爲覆蓋。
我想這樣做,當在單元格/滾動視圖上有任何位置的平移/滑動時,則會檢測到平移。
但是,如果在button
上有一個水龍頭,則觸發targerButton
,如果它在外部targetCell
被觸發。
到目前爲止,只有targetCell
被觸發,位於button
區域的內部或外部。如果我刪除UIScrollView
,它工作正常。
所以起初我想過重寫func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool
。問題是,在這個階段,它並沒有在水龍頭和水龍頭之間產生差異,所以它不能解決我的問題。
然後我想到了覆蓋func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
類似如下:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let location = touches.first?.locationInView(tableView) {
var buttonFrame = CGRectMake(frame.width - 50, 0, 50, bounds.height)
buttonFrame.origin.y = (convertRect(buttonFrame, toView: tableView).minY)
let toNextResponder = buttonFrame.contains(location)
if toNextResponder {
self.nextResponder()?.touchesBegan(touches, withEvent: event)
} else {
super.touchesBegan(touches, withEvent: event)
}
} else {
super.touchesBegan(touches, withEvent: event)
}
}
它沒有工作。然後我意識到,self.nextResponder()?
是UITableViewCellContentView
,所以我想也許,直接撥打touchesBegan(touches, withEvent: event)
button
。所以我改變touchesBegan
這樣:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let location = touches.first?.locationInView(tableView) {
var buttonFrame = CGRectMake(frame.width - 50, 0, 50, bounds.height)
buttonFrame.origin.y = (convertRect(buttonFrame, toView: tableView).minY)
let toNextResponder = buttonFrame.contains(location)
if toNextResponder {
if let cellContentView = self.nextResponder() as? UIView {
let button = findButtonInView(cellContentView)
button?.touchesBegan(touches, withEvent: event)
}
} else {
super.touchesBegan(touches, withEvent: event)
}
} else {
super.touchesBegan(touches, withEvent: event)
}
}
private func findButtonInView(view: UIView) -> UIButton? {
if view is UIButton {
return view as? UIButton
} else {
var button: UIButton?
for subview in view.subviews {
button = findButtonInView(subview)
if let _ = button {
return button
}
}
return button
}
}
而且......這是行不通的,只有targetCell
被觸發,你是否在button
或沒有自來水。我注意到一些事情,當我打電話給chartButton?.touchInside
時,我得到了false
。
我做錯了什麼?或者我應該做什麼?