Domanda

I'm trying to fade my annotations in through animation, so I create them with an alpha of 0, and then animate their alpha to 1 in didAddAnnotation. Sometimes I'm only adding a few annotations, subtracting others, but right now EVERY annotation on screen is being faded out/in when ANY are added rather than the expected behavior, which would be that only the recently added pins would fade to 1.

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{

for (MKAnnotationView *view in views){

        if (view.alpha ==0){

            [UIView animateWithDuration:.5 animations:^{

                view.alpha = 1;

            }];
        }

}
È stato utile?

Soluzione

For animating the annotations as its added, you can use this subclass of MKAnnotationView

class AnnotationView: MKAnnotationView {

    override var annotation: MKAnnotation? {
        didSet {
            UIView.animate(withDuration: 0.5) { self.alpha = 1.0 }
        }
    }

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        self.alpha = 0
    }

    override func prepareForReuse() {
        super.prepareForReuse()

        self.alpha = 0.0
    }

}

Usage:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if let view = mapView.dequeueReusableAnnotationView(withIdentifier: "annot") as? AnnotationView {
        return view
    }

    return AnnotationView(annotation: annotation, reuseIdentifier: "annot")
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top