【发布时间】:2019-01-14 16:22:44
【问题描述】:
正如标题描述的问题,我不知道触发链接的函数或方法。
我已经将所有注释添加到地图中。
希望有人能给出具体的示例代码,谢谢。
【问题讨论】:
-
嗨,欢迎来到 Stack。很高兴看到您尝试过的内容以及遇到的任何错误?发布一些代码,有人可能会帮助您使其工作。
标签: swift scroll uicollectionview annotations
正如标题描述的问题,我不知道触发链接的函数或方法。
我已经将所有注释添加到地图中。
希望有人能给出具体的示例代码,谢谢。
【问题讨论】:
标签: swift scroll uicollectionview annotations
一些想法:
您的代码显示:
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。
您选择注释的代码似乎也不正确。你正在做:
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)
}
}
这实际上是说“如果所选注释是我们想要的注释,则选择它。”我认为你想要相反的,即:
【讨论】: