nor0x
nor0x

Reputation: 1213

Transform MKAnnotationView without Callout

I try to scale up the view of a MKAnnotation on
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) As you can see in the animation, the problem is that not only the Pin part of the MKAnnotation, but also is the Callout is scaled - anybody knows how to fix this?

Screenshot

I use the following code for the scaling animation (it's an extension for UIView)

func scaleUp(duration: TimeInterval = 0.2) {
    self.transform = CGAffineTransform.identity
    UIView.animate(withDuration: duration, delay: 0.0, options: [.curveLinear], animations: { () -> Void in
        self.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
    }) { (animationCompleted: Bool) -> Void in }
}

Upvotes: 2

Views: 435

Answers (1)

Jakub Truhlář
Jakub Truhlář

Reputation: 20720

That is because the callout view is MKAnnotationView's subview. Unfortunately you can't access the callout view hence there is no way to counter that transformation - Well you could find the callout view in the hierarchy, but it doesn't work as you would need.

But you could approach it from the opposite direction:

  • Start with lower scale (this will act as the default size), let's say 0.5, and scale it up to 1.0 (in this moment u have selected pin with standard size callout view).

  • All you need is proper sized image for the default value (in this case the default image size must be bigger).

If you're using clustering:

The MapKit framework internally changes the transformation matrix for the translation purpose (The pin is moving to/from the cluster). This has one side effect, it changes a and d in the transformation matrix to 1.0 which means it resets the scale you set previously. To solve that, override the transform in the annotation view like so:

override var transform: CGAffineTransform {
    get {
        return super.transform
    }
    set {
        super.transform = CGAffineTransform(
            a: isSelected ? 1.0 : 0.5,
            b: newValue.b,
            c: newValue.c,
            d: isSelected ? 1.0 : 0.5,
            tx: newValue.tx,
            ty: newValue.ty)
    }
}

Upvotes: 2

Related Questions