2017-04-05 57 views
0

我在swift中构建了一个应用程序,该应用程序在同一页面上具有集合视图和地图视图。用户可以滚动浏览收藏视图以查看优惠(这是应用的目的),并且地图视图中填充了注释引脚,用于在地图上显示优惠的实际位置。需要帮助将mapKit注释链接到Swift中的CollectionView

我的问题是,我发现很难收集观察室链接到正确的注释。

我的注释添加到地图如下:

for location in self.queryDataMapAnnotations { 
      let annotation = MKPointAnnotation() 
      annotation.title = location["title"] as? String 
      annotation.subtitle = location["distance"] as? String 
      annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["longitude"] as! Double) 
      self.mapView.addAnnotation(annotation) 

     } 

我则可以通过收集的滚动视图细胞

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     // get a reference to our storyboard cell 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell 


     // Connect cell to the linked annotation 

     // ** This links to a random annotation, but not the correct one - need help on this please ** 
     self.mapView.selectAnnotation(self.mapView.annotations[row], animated: true) 

    } 

如果任何人能帮助我如何收集链接查看正确的地图注释,我会是最有帮助的。我只知道很快(一点点),所以会欣赏那种语言的帮助。由于

+0

你的目的,突出标注时收集细胞是** **螺孔?因为目前您在错误的集合视图dataSource方法中调用'selectAnnotation()'。 – nshuman

+0

不,用途是在用户滚动浏览集合视图时选择注释。因此,当用户滚动到第1个集合视图单元格时,会选中相应的注释并显示标题和副标题。然后,当用户滚动到第二个单元格,第三个等时,这继续下去。 –

回答

0

如果你想活动地图标注更改为你滚动,你要考虑垂直居中收集细胞是当前显示的 - 这里是我想出了办法。

注:我没有测试它,它只是

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    if scrollView is UICollectionView { 
     let collectionViewCenter = CGPoint(x: collectionView.bounds.size.width/2 + collectionView.contentOffset.x, y: collectionView.bounds.size.height/2 + collectionView.contentOffset.y) 
     let centredCellIndexPath = collectionView.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 
     } 

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

这与在cellForItemAt委托方法中使用'selectAnnotation'执行相同的功能。问题在于,您的解决方案和我的解决方案中选择的注释不正确。所以当单元格1在视图中时,注释3被选中。滚动到单元格2会选择注释5(随机示例,但您可以看到图片,这是因为注释和单元格数据未对齐 - 所以我需要创建一种将两者同步的方法,但我不确定如何。对此的任何想法是非常受欢迎的。再次感谢 –

+0

我实际上认为我做错了,请检查更新的答案并给它一个机会 – nshuman

+0

@StevenH它可能实际上需要对此行进行一次更改:'let collectionViewCenter = CGPoint(x:collectionView.bounds.size.width/2 + collectionView.contentOffset.x,y:collectionView.bounds.size.height/2 + collectionView.contentOffset.y)'。不确定,可以在这里测试是否正确现在, – nshuman

0

我也有类似的功能,用户滚动,显示附近的优惠和MapView类的相机应该转移到列表中的CollectionView的基本思路在集合视图当前可见的交易位置,下面介绍我的实现

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>){ 
     /* set pagewidth to collectionview cell width*/ 
     let pageWidth = collectionView.bounds.width 
     let currentOffset : Float = Float(scrollView.contentOffset.x) 
     let targetOffset = Float(targetContentOffset.memory.x) 
     var newTargetOffset : Float = 0.0 

     if targetOffset > currentOffset { 
      newTargetOffset = ceilf(currentOffset/(pageWidth)) * pageWidth 
      /* if colelctionview swipped right increment the curentlyVisibleIndex*/ 
     //dealList is array of Model objects used to populate the 
       //CollectionView 
      if currentlyVisibleIndex != (dealList.count - 1) { 
       currentlyVisibleIndex += 1 
      } 
     } 
     else{ 
      newTargetOffset = floorf(currentOffset/(pageWidth)) * pageWidth 
      /*if collectionview swipped left decrement the currentlyVisibleCounter */ 
      if currentlyVisibleIndex != 0 { 
       currentlyVisibleIndex -= 1 
      } 
     } 
     if newTargetOffset < 0{ 
      newTargetOffset = 0 
     }else if newTargetOffset > Float(scrollView.contentSize.width){ 
      newTargetOffset = Float(scrollView.contentSize.width) 
     } 
     targetContentOffset.memory.x = CGFloat(currentOffset) 
     scrollView.setContentOffset(CGPoint(x: CGFloat(newTargetOffset), y: 0), animated: true) 

     moveCameraToDealLocation() 
     // re initialise the annotations of mapView 
     let annotationArray = mapBoxViewController.mapView.annotations 
     mapBoxViewController.mapView.removeAnnotations(annotationArray) 
     mapBoxViewController.mapView.addAnnotations(annotationArray) 
    } 

    func moveCameraToDealLocation(){ 
     mapBoxViewController.moveMapViewCameraToLocation(CLLocationCoordinate2D(latitude: dealList[currentlyVisibleIndex].eventLatitude, longitude: dealList[currentlyVisibleIndex].eventLongitude)) 
    } 

currentlyVisibleIndex给我当前可见的交易(注释)的指数,functi moveCameraToDealLocation将MapView相机移动到该注释。 不要让我知道,如果这对你的作品:)