如何检测 UITableView 上的触摸,到目前为止似乎没有任何效果?
How to detect touches on UITableView, nothing seems to work so far?
如何检测 UITableView
上的触摸?我尝试了这个 thread 中的解决方案。我的 tableView
和一个 scrollView
在同一个 UIViewController
中并且有一个约束,所以子类化 tableView
.
很复杂
未调用触摸方法:
class ViewController: UIViewController, UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var verticalSpacing: NSLayoutConstraint!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var tableView: UITableView!
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
println("touch began")
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
super.touchesMoved(touches, withEvent: event)
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
println("touch ended")
}
override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
super.touchesCancelled(touches, withEvent: event)
}
我补充了:
tableView.userInteractionEnabled = true
tableView.canCancelContentTouches = false //or true is the same
但这并没有改变任何东西。 scrollView
与 tableView
同时可见,但它们不重叠。
你的问题是,touchesBegan
等也是来自 UIView
的方法,所以你需要从 class 到 UITableView
或使用 TapGestureRecognizer 来实现按需工作。
子class UITableView
应该不会太复杂,因为您只需将 UITableView class 设置为 Storyboard 中 tableView 的 class ,就像你对 UIViewControllers 等做的那样
有一些方法在使用 UITableView 时很有用,可以检测滚动视图的变化。这些可能对你有用。
例如,
override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
println("Ended decelerating")
}
override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
println("Ended dragging")
}
您尝试调整 delaysContentTouches
了吗?可能是因为如果您按下的时间足够长,您的触摸会以默认延迟注册。
请问,有什么理由不能只用didSelectRowAtIndexPath
?
不需要子类化!
只需添加手势识别器并确保包含对 cancelsTouchesInView
的调用
这是 Swift 版本:
override func viewDidLoad() {
super.viewDidLoad()
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))
}
func handleTap(sender: UITapGestureRecognizer) {
if sender.state == .Ended {
// Do your thang here!
}
sender.cancelsTouchesInView = false
}
尽情享受吧!
晚会迟到了,但我需要检测触摸 而不是在 table 上点击 所以我结束了这个:
fileprivate class TouchSensitiveTableView: UITableView {
fileprivate var onTouchStart: (() -> ())? = nil
fileprivate var onTouchEnd: (() -> ())? = nil
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.onTouchStart?()
}
public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.onTouchEnd?()
}
}
然后这样使用
private let tableView = TouchSensitiveTableView()
viewDidLoad() {
....
self.tableView.onTouchStart = {[weak self] in .... }
self.tableView.onTouchEnd = {[weak self] in .... }
与 table 的滚动有轻微交互,但不重要。
如何检测 UITableView
上的触摸?我尝试了这个 thread 中的解决方案。我的 tableView
和一个 scrollView
在同一个 UIViewController
中并且有一个约束,所以子类化 tableView
.
很复杂
未调用触摸方法:
class ViewController: UIViewController, UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var verticalSpacing: NSLayoutConstraint!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var tableView: UITableView!
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
println("touch began")
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
super.touchesMoved(touches, withEvent: event)
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
println("touch ended")
}
override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
super.touchesCancelled(touches, withEvent: event)
}
我补充了:
tableView.userInteractionEnabled = true
tableView.canCancelContentTouches = false //or true is the same
但这并没有改变任何东西。 scrollView
与 tableView
同时可见,但它们不重叠。
你的问题是,touchesBegan
等也是来自 UIView
的方法,所以你需要从 class 到 UITableView
或使用 TapGestureRecognizer 来实现按需工作。
子class UITableView
应该不会太复杂,因为您只需将 UITableView class 设置为 Storyboard 中 tableView 的 class ,就像你对 UIViewControllers 等做的那样
有一些方法在使用 UITableView 时很有用,可以检测滚动视图的变化。这些可能对你有用。
例如,
override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
println("Ended decelerating")
}
override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
println("Ended dragging")
}
您尝试调整 delaysContentTouches
了吗?可能是因为如果您按下的时间足够长,您的触摸会以默认延迟注册。
请问,有什么理由不能只用didSelectRowAtIndexPath
?
不需要子类化!
只需添加手势识别器并确保包含对 cancelsTouchesInView
这是 Swift 版本:
override func viewDidLoad() {
super.viewDidLoad()
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))
}
func handleTap(sender: UITapGestureRecognizer) {
if sender.state == .Ended {
// Do your thang here!
}
sender.cancelsTouchesInView = false
}
尽情享受吧!
晚会迟到了,但我需要检测触摸 而不是在 table 上点击 所以我结束了这个:
fileprivate class TouchSensitiveTableView: UITableView {
fileprivate var onTouchStart: (() -> ())? = nil
fileprivate var onTouchEnd: (() -> ())? = nil
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.onTouchStart?()
}
public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.onTouchEnd?()
}
}
然后这样使用
private let tableView = TouchSensitiveTableView()
viewDidLoad() {
....
self.tableView.onTouchStart = {[weak self] in .... }
self.tableView.onTouchEnd = {[weak self] in .... }
与 table 的滚动有轻微交互,但不重要。