【问题标题】:How to make links of map annotation to ui collection cell when scrolling the cell highlight the annotation滚动单元格时如何使地图注释链接到ui集合单元格突出显示注释
【发布时间】:2019-01-14 16:22:44
【问题描述】:

正如标题描述的问题,我不知道触发链接的函数或方法。

我已经将所有注释添加到地图中。

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

【问题讨论】:

  • 嗨,欢迎来到 Stack。很高兴看到您尝试过的内容以及遇到的任何错误?发布一些代码,有人可能会帮助您使其工作。

标签: swift scroll uicollectionview annotations


【解决方案1】:

一些想法:

  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 应用程序)与集合视图的大小不同,您可能会尝试调用 indexPathForItem(at:) 以获得集合视图的 bounds 之外的 CGPoint,并且不会得到在那种情况下的任何命中。

    因此,例如,如果您想要集合视图的中心,您可以将 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)
        }
    }
    

    这实际上是说“如果所选注释是我们想要的注释,则选择它。”我认为你想要相反的,即:

    • 找到应该选择的注释;
    • 如果未选择,请选择。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多