ios - 忽略 UICollectionView 空白处的触摸/手势

标签 ios swift uicollectionview

我在 UICollectionsView 下有一个 map View (Google Maps 而不是 MapKit),我想允许平移/缩放 map ,但是由于 UICollectionView 设置为占据整个屏幕,它会拦截所有触摸事件,使 map 保持静止。

作为引用,这就是我目前所拥有的,我将 UICollectionView 设置为全屏的原因是这样我就可以在所有屏幕空间内分别为单元格的位置设置动画。

enter image description here

我试过重写 this answer在 Swift 中,但它在展开时抛出一个 nil :

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    let indexPath: IndexPath? = self.indexPathForItem(at: point)
    let cell: UICollectionViewCell? = (self.cellForItem(at: indexPath!))
    if (cell != nil) && convert(point, to: cell?.contentView).x >= 0 {
        return true
    }
    return false
}

最佳答案

想通了。我没有检测触摸事件是否发生在单元格内,而是检查事件是否发生在某个 y 点之后并进行相应处理。

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    if point.y > cardContainerY {
        return true
    }
    return false
}

cardContainerY 是一个 CGFloat,它是根据当前聚焦的卡片计算出来的(因为卡片可以展开,在 y 空间内移动).

关于ios - 忽略 UICollectionView 空白处的触摸/手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42603089/

相关文章:

ios - 以编程方式使用自动布局将 subview 添加到 UICollectionView 不起作用

ios - 彩色 UIImages 变得像素化

ios - Xcode 10.2更新问题构建系统错误-1 : Unable to load contents of file list: input/output xcfilelist

iphone - iOS 通用应用程序 : the Minimum OS Version is Different than the Deployment Target

ios - UITableView 最后一行被截断

xcode - UIViewController 在 iOS 8 中不显示(但在 iOS 9 中可用)

ios - 在 Swift 中获取表格内容的高度

ios - 在 UISplitView 中替换我的 detailViewController 在旋转我的应用程序 Objective-C 时崩溃

swift - 在 Collection View 之外有一个按钮来执行 Collection View 单元格应该执行的操作

ios - Swift - 当我尝试通过 cellForItem 访问项目时,我的项目为零