【问题标题】:How to detect screen location on UICollectionView?如何检测 UICollectionView 上的屏幕位置?
【发布时间】:2016-02-07 02:58:10
【问题描述】:

应用程序需要在选择UICollectionView单元格时获取屏幕位置(x,y)。

我曾尝试过touchesBegan,但它无法正常工作。我还用UITapGestureRecognizer 和gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool 进行了测试,当设置它true 或false 时,它可以通过手势或点击单元格来工作,而不是同时使用。

有什么方法可以让这两个动作同时工作?

谢谢。

【问题讨论】:

  • “手势或点击”是什么意思?另一个手势是什么?
  • @beyowulf 这意味着当手势 shouldReceiveTouch 设置为 true 时,会调用手势,而不会调用集合单元格 didSelectItemAtIndexPath 。当它设置为 false 时,集合单元格 didSelectItemAtIndexPath 被调用并且手势不会。

标签: ios uicollectionview touch gesture tap


【解决方案1】:

我不确定你为什么需要同时开火。无论您在其中做什么,您都可以将该代码移至另一个。但要回答你的问题,你可以做类似的事情。

不要使用 didSelectItemAtIndexPath。将该代码移动到gestureRecognizer。如果您需要水龙头的 indexPath,请说:

let tapLocation = gesture.location(in: self.view)
let indexPath = collectionView.indexPathForItem(at: tapLocation)

如果你不喜欢,你可以尝试实现

optional func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool

并返回 true 以查看是否允许调用这两种方法。

【讨论】:

  • 第二个不行,但是第一个很好用!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 2010-11-13
  • 1970-01-01
  • 2018-02-09
  • 2014-10-19
  • 2021-04-16
  • 2016-10-29
相关资源
最近更新 更多