2017-04-05 46 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相機移動到該註釋。 不要讓我知道,如果這對你的作品:)