在 tableview 顶部滑动视图时未调用 touchesEnded
touchesEnded not called when swiping a view on top of a tableview
我有一个简单的 table 视图,其中只有包含文本的行(单元格是标准的 UITableViewCell,我只是修改了它们的 .textLabel 属性)。
我以编程方式向 table 视图添加了一个 UIView,并使其在 table 上方看起来像一个红色方块。我还为这个方形视图分配了一个 UIPanGestureRecognizer。这个想法是能够将正方形视图拖到整个 table 视图。
我可以看到(覆盖的)函数 touchesBegan 和 touchesCancelled 是如何正常调用的。 touchesEnded 只有在我不滑动正方形视图时才会被调用,这只是点击,没有拖动。
通过阅读类似的帖子,我了解到问题是 UIGestureRecognizer 正在识别滑动,这会覆盖 touchesEnded,但我不知道如何处理这个问题。
非常感谢您的帮助!
更新以包含代码。
来自 TableViewController。 Token是我要拖拽的自定义视图(之前提到的"square view")。
override func viewDidAppear(animated: Bool) {
let token = Token(frame: CGRect(x: 10, y: 10, width: 20, height: 20), color: UIColor.redColor())
self.view.addSubview(token)
self.view.bringSubviewToFront(token)
}
令牌,我的自定义视图:
class Token: UIView {
var lastLocation:CGPoint = CGPoint(x: 10, y: 10)
init(frame: CGRect, color: UIColor) {
super.init(frame: frame)
self.backgroundColor = color
let panRecognizer = UIPanGestureRecognizer(target:self, action:"detectPan:")
self.gestureRecognizers = [panRecognizer]
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func detectPan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(self.superview!)
self.center = CGPointMake(lastLocation.x + translation.x, lastLocation.y + translation.y)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("began")
// Promote the touched view
self.superview?.bringSubviewToFront(self)
// Remember original location
lastLocation = self.center
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touchesEnded")
}
}
UIPanGestureRecognizer
上有 属性 cancelsTouchesInView
。将其设置为 NO
,它会将触摸传递到下方 UIView
。
然后您可以实现 touchesBegan
功能来移动您的视图。
我有一个简单的 table 视图,其中只有包含文本的行(单元格是标准的 UITableViewCell,我只是修改了它们的 .textLabel 属性)。
我以编程方式向 table 视图添加了一个 UIView,并使其在 table 上方看起来像一个红色方块。我还为这个方形视图分配了一个 UIPanGestureRecognizer。这个想法是能够将正方形视图拖到整个 table 视图。
我可以看到(覆盖的)函数 touchesBegan 和 touchesCancelled 是如何正常调用的。 touchesEnded 只有在我不滑动正方形视图时才会被调用,这只是点击,没有拖动。
通过阅读类似的帖子,我了解到问题是 UIGestureRecognizer 正在识别滑动,这会覆盖 touchesEnded,但我不知道如何处理这个问题。
非常感谢您的帮助!
更新以包含代码。
来自 TableViewController。 Token是我要拖拽的自定义视图(之前提到的"square view")。
override func viewDidAppear(animated: Bool) {
let token = Token(frame: CGRect(x: 10, y: 10, width: 20, height: 20), color: UIColor.redColor())
self.view.addSubview(token)
self.view.bringSubviewToFront(token)
}
令牌,我的自定义视图:
class Token: UIView {
var lastLocation:CGPoint = CGPoint(x: 10, y: 10)
init(frame: CGRect, color: UIColor) {
super.init(frame: frame)
self.backgroundColor = color
let panRecognizer = UIPanGestureRecognizer(target:self, action:"detectPan:")
self.gestureRecognizers = [panRecognizer]
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func detectPan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(self.superview!)
self.center = CGPointMake(lastLocation.x + translation.x, lastLocation.y + translation.y)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("began")
// Promote the touched view
self.superview?.bringSubviewToFront(self)
// Remember original location
lastLocation = self.center
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("touchesEnded")
}
}
UIPanGestureRecognizer
上有 属性 cancelsTouchesInView
。将其设置为 NO
,它会将触摸传递到下方 UIView
。
然后您可以实现 touchesBegan
功能来移动您的视图。