如何在滚动单元格时将地图批注链接到ui集合单元格以高亮显示批注



由于标题描述了这个问题,我不知道该用哪个函数或方法来触发链接。

我已经在地图上添加了所有的注释。

希望有人能给出具体的示例代码,谢谢。

几个想法:

  1. 您的代码显示:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView is UICollectionView {
    let collectionViewCenter = CGPoint(x: myMap.bounds.size.width / 2 + myMap.frame.minX, y: myMap.bounds.size.height / 2 + myMap.frame.minY)
    let centredCellIndexPath = cafeCollectionView.indexPathForItem(at: collectionViewCenter)
    guard let path = centredCellIndexPath else {
    // There is no cell in the center of collection view (you might want to think what you want to do in this case)
    return
    }
    // select annotation here, if needed
    }
    }
    

    您将collectionViewCenter设置为映射的中心,但随后将其用作indexPathForItem(at:)的参数。如果要查找centeredCellIndexPath,则应使用集合视图的bounds的中心,而不是映射的中心。

    如果你的地图和AirBnb应用程序一样,大小与集合视图不同,那么你可能试图为集合视图的bounds之外的CGPoint调用indexPathForItem(at:),在这种情况下不会得到任何命中。

    因此,例如,如果您想要集合视图的中心,您可以将collectionViewCenter赋值替换为以下内容:

    let collectionViewCenter = CGPoint(x: cafeCollectionView.bounds.midX,
    y: cafeCollectionView.bounds.midY)
    

    或者,如果您想在左边缘获得该单元格,则使用一些固定的x值,而不是midX

  2. 您选择注释的代码似乎也不对。你正在做:

    if let selectedAnnotation = myMap.selectedAnnotations.first {
    // Ignore if correspondent annotation is already selected
    if selectedAnnotation.isEqual(self.myMap.annotations[path.row]) {
    self.myMap.selectAnnotation(self.myMap.annotations[path.row], animated: true)
    }
    }
    

    这实际上意味着"如果所选的注释是我们想要的,那么就选择它。"我认为你想要的是相反的,即:

    • 查找应选择的注释
    • 如果未选中,请执行此操作

最新更新