###前提・実現したいこと
タイトルの通りになります。
MapView
とCollectionView
が連動する形の画面を作りたいと思っています。
その際、ピンタップ時および、collectionView
でページめくりした際に、
対象のアノテーションへズームし、ピン画像を指定したものに変えるというものです。
ページスクロール終了を検知したさいにcollectionView上の名前
とアノテーションのタイトル
が一致した際に
ピンを変えようと思い処理を書いてみたのですがうまく画像が切り替わりません。
###補足情報(言語/FW/ツール等のバージョンなど)
・Xcode7.3.1
・Swift2.2
###該当のソースコード(抽出)
Swift
1func scrollViewDidEndDecelerating(scrollView: UIScrollView) { 2 if fmod(scrollView.contentOffset.x, scrollView.frame.maxX) == 0 { 3 currentPage = Int(scrollView.contentOffset.x / scrollView.frame.maxX) 4 for i in 0..<mapView.annotations.count { 5 if mapView.annotations[i].title! == data[currentPage]["name"] as? String { 6 let identifier = "annotation" 7 let annotationView = MKAnnotationView(annotation: self.mapView.annotations[i], reuseIdentifier: identifier) 8 annotationView.image = UIImage(named: "sample") 9 } 10 } 11 let span = MKCoordinateSpanMake(0.01, 0.01) 12 let coordinate = CLLocationCoordinate2DMake((data[currentPage]["latitude"] as? Double)! , (data[currentPage]["longitude"] as? Double)!) 13 let region = MKCoordinateRegionMake(coordinate, span) 14 mapView.setRegion(region, animated:true) 15 } 16 }
###該当のソースコード(全文)
Swift
1class ViewController2: UIViewController, MKMapViewDelegate, UICollectionViewDataSource { 2 3 @IBOutlet weak var mapView: MKMapView! 4 @IBOutlet weak var collectionView: UICollectionView! 5 6 private var beforePage = 0 7 private var currentPage = 0 8 var data: [[String:AnyObject?]] = [] 9 10 override func viewWillAppear(animated: Bool) { 11 super.viewWillAppear(animated) 12 self.navigationItem.title = "概要" 13 } 14 15 override func viewDidAppear(animated: Bool) { 16 super.viewDidAppear(animated) 17 alamoNetwork() 18 } 19 20 func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 21 22 let customAnnotation = annotation as! MapAnnotationSetting 23 24 let identifier = "annotation" 25 if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("annotation") { 26 return annotationView 27 } else { 28 if customAnnotation.pinImage != nil { 29 let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) 30 annotationView.annotation = annotation 31 annotationView.canShowCallout = false 32 annotationView.image = UIImage(named: (customAnnotation.pinImage)!) 33 34 return annotationView 35 } else { 36 return nil 37 } 38 } 39 } 40 41 func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 42 for i in 0..<data.count { 43 let title = view.annotation?.title 44 if title! == data[i]["name"] as? String { 45 view.image = UIImage(named: "sample") 46 let span = MKCoordinateSpanMake(0.01, 0.01) 47 let coordinate = CLLocationCoordinate2DMake((data[i]["latitude"] as? Double)! , (data[i]["longitude"] as? Double)!) 48 let region = MKCoordinateRegionMake(coordinate, span) 49 mapView.setRegion(region, animated:true) 50 } 51 } 52 } 53 54 func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) { 55 let customAnnotation = view.annotation as! MapAnnotationSetting 56 view.image = UIImage(named: (customAnnotation.pinImage)!) 57 } 58 59 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 60 return data.count 61 } 62 63 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 64 let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CustomCell 65 if data.count > 0 { 66 cell.shopName.text = data[indexPath.row]["name"] as? String 67 } 68 return cell 69 } 70 71 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 72 let width = collectionView.frame.width 73 let height = collectionView.frame.height 74 75 return CGSize(width: width, height: height) 76 } 77 78 func scrollViewWillBeginDragging(scrollView: UIScrollView) { 79 beforePage = Int(scrollView.contentOffset.x / scrollView.frame.maxX) 80 } 81 /* ーーーーーーーーこの部分に記載してありますーーーーーーーー*/ 82 func scrollViewDidEndDecelerating(scrollView: UIScrollView) { 83 if fmod(scrollView.contentOffset.x, scrollView.frame.maxX) == 0 { 84 currentPage = Int(scrollView.contentOffset.x / scrollView.frame.maxX) 85 for i in 0..<mapView.annotations.count { 86 if mapView.annotations[i].title! == data[currentPage]["name"] as? String { 87 let identifier = "annotation" 88 let annotationView = MKAnnotationView(annotation: self.mapView.annotations[i], reuseIdentifier: identifier) 89 annotationView.image = UIImage(named: "sample") 90 } 91 } 92 let span = MKCoordinateSpanMake(0.01, 0.01) 93 let coordinate = CLLocationCoordinate2DMake((data[currentPage]["latitude"] as? Double)! , (data[currentPage]["longitude"] as? Double)!) 94 let region = MKCoordinateRegionMake(coordinate, span) 95 mapView.setRegion(region, animated:true) 96 } 97 } 98 /* ーーーーーーーーーーーーここまでーーーーーーーーーーーー*/ 99 100 func dropPin() { 101 if self.data.count > 0 { 102 for i in 0..<data.count { 103 let latitude = self.data[i]["latitude"] as? Double 104 let longitude = self.data[i]["longitude"] as? Double 105 106 let annotation = MapAnnotationSetting() 107 annotation.coordinate = CLLocationCoordinate2DMake(latitude!, longitude!) 108 annotation.title = self.data[i]["name"] as? String 109 annotation.pinImage = "map_01" 110 111 mapView.addAnnotation(annotation) 112 } 113 mapView.showAnnotations(mapView.annotations, animated: true) 114 } 115 } 116 117 func alamoNetwork() { 118 ※省略※ 119 } 120 121} 122
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/09 01:45
2016/11/09 01:52 編集
2016/11/09 02:00
2016/11/09 02:10
2016/11/09 02:34 編集
2016/11/09 02:49
2016/11/09 02:55 編集
2016/11/09 03:03
2016/11/09 03:12