更改didAddAnnotation中添加的MKAnnotationView alpha会影响屏幕上的所有MKAnnotationViews [英] changing added MKAnnotationView alpha in didAddAnnotation affects all MKAnnotationViews on screen

查看:75
本文介绍了更改didAddAnnotation中添加的MKAnnotationView alpha会影响屏幕上的所有MKAnnotationViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过动画淡入注释,所以我将其alpha设置为0,然后在didAddAnnotation中将其alpha设置为1.有时我只添加一些注解,减去其他注解,但是现在,当添加ANY而不是预期的行为时,屏幕上的每个注解都将逐渐淡入/淡入,这是因为只有最近添加的图钉会淡化为1.

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;

            }];
        }

}

推荐答案

要对添加的注释进行动画处理,可以使用 MKAnnotationView

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
    }

}

用法:

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")
}

这篇关于更改didAddAnnotation中添加的MKAnnotationView alpha会影响屏幕上的所有MKAnnotationViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆