NoSixties
NoSixties

Reputation: 2533

Perform didSelect before didDeselect

The title might not due the actually question justice since there might be a completely different solution to this problem.

What we are trying to do is create a snapchat like map view, where data is displayed on the bottom of the screen in a collection view when you press a certain Annotation. When a annotation is selected either in the collection view or by tapping the annotation. The selected annotation will be bigger then all the other annotations. To do all of this our didSelect and didDeselect delegate methods look like below

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if view.annotation!.isKind(of: MKUserLocation.self) {
        return
    }

    if view.isSelected {
        view.transform = CGAffineTransform(scaleX: annotationSizeMultiplier, y: annotationSizeMultiplier)
        }

    if !isVisible {
        isVisible = true
        UIView.animate(withDuration: 0.25, animations: {
            self.collectionViewBackground.frame.size.height += self.height
            self.collectionViewBackground.frame.origin.y -= self.height
            self.customCallout.frame = self.collectionViewBackground.frame
        }, completion: { _ in
            self.customCallout.showCollectionCell(self, annotationTitle: ((view.annotation?.title)!)!)
        })
    } else {
        customCallout.showCollectionCell(self, annotationTitle: ((view.annotation?.title)!)!)
    }
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    if !view.isSelected {
        view.transform = CGAffineTransform.identity
    }
}

however what we want to do is hide the collection view when you tap somewhere on the map view where no annotation is present. However the problem we are having is that we don't actually know if the user selected another annotation or just tapped randomly in the map view, since the didDeselect is being called before the didSelect is.

we did fix this problem with a very hacky solution which we aren't actually pleased with, which you can see below.

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    if !view.isSelected {
        view.transform = CGAffineTransform.identity
    }

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
        if mapView.selectedAnnotations.count == 0 {
            self.isVisible = false
            UIView.animate(withDuration: 0.25, animations: {
                self.collectionViewBackground.frame.size.height -= self.height
                self.collectionViewBackground.frame.origin.y += self.height
                self.customCallout.frame = self.collectionViewBackground.frame
            })
        }
    })
}

We're hoping someone knows a better solution to actually solve this the "correct" way.

Upvotes: 3

Views: 396

Answers (0)

Related Questions