MKMapView,animateDrop? [英] MKMapView, animateDrop?

查看:130
本文介绍了MKMapView,animateDrop?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个NSMutableArray对象,该对象从符合MKAnnotation协议的类派生。我有设置安装标题和字幕的注释,并成功地添加到MKMapView使用:

  [[self customMapView] addAnnotations :locationArray]; 

我现在想做的是使引脚放置动画,最初我想我可以用 Option1 ,使用这一切工作,但引脚不做动画下降。

  // Option1 
// WORKS FOR:pinColor YES,animatesDrop NO,LABEL YES
- void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
for(MKPinAnnotationView * eachView in views){
[eachView setAnimatesDrop:YES];
[eachView setPinColor:MKPinAnnotationColorPurple];
}
}

我的下一个猜测是尝试 Option2 ,这似乎工作非常好,但我有两个问题。标题和字幕没有显示,我的自定义注释对象被传入(我可以在调试器中看到它),但包含的信息不是跨越到新的 pin 。其次,这将创建一组新的MKAnnotationViews,旧的,发生了什么,是否有和内存泄漏的问题?

  // Option2 
// FOR:pinColor YES,animatesDrop YES,LABEL NO
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id< MKAnnotation>)annotation {
MKPinAnnotationView * pin =(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@annotation_ID];
if(pin == nil){
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@annotation_ID] autorelease];
} else {
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = YES;
return pin;
}

编辑:我已经解决了缺少的标题&添加 pin.canShowCallout = YES;

解决方案

$ M $ n $ n $ $

MKPinAnnotationView是MKAnnotationView的一个方便的子类,它自动提供所选颜色的图钉图像和将图钉拖放到地图上的动画。



如果你不实现viewForAnnotation,一个标准的红色别针没有



在调用didAddAnnotationViews时,自动动画已经发生,并且设置该属性没有任何效果。



然而,如果你想创建一个不同于MKPinAnnotationView提供的默认drop动画的自定义动画,你可以在didAddAnnotationViews中做。视图已经在其最终目标点,因此您保存它,然后将其从不同的点动画到该目标。



如果您对默认下降感到满意MKPinAnnotationView提供的动画,你不需要实现didAddAnnotationViews。这个委托方法对于在所有注释视图实际就位时可能需要做的其他事情更有用。



为了让你的引脚显示标题,设置 canShowCallout 您在此设置 animatesDrop



不确定这创建了一组新的MKAnnotationViews是什么意思。在viewForAnnotation方法中,您将为MKAnnotation对象提供视图(MKPinAnnotationView)。他们是不一样的东西。



此外,viewForAnnotation方法的工作方式类似于UITableView的cellForRowAtIndexPath方法,其中注释视图可以循环使用,这就是为什么设置MKAnnotation-信息(例如注释属性)。


I have setup an NSMutableArray of objects derived from a class that conforms to the MKAnnotation protocol. I have setup setup title and subtitle for the annotation and have successfully added them to the MKMapView using:

[[self customMapView] addAnnotations:locationArray];

What I want to do now is animate the pins dropping, initially I thought I could do that with Option1, using this everything works, but the pins don't do an animated drop.

// Option1
// WORKS FOR: pinColor YES, animatesDrop NO, LABEL YES
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    for(MKPinAnnotationView *eachView in views) {
        [eachView setAnimatesDrop:YES];
        [eachView setPinColor:MKPinAnnotationColorPurple];
    }
}

My next guess was to try Option2, this seems to work very well but I have two issues. The title and subtitle don't show up, my custom annotation object is getting passed in (I can see it in the debugger) but the information contained is not making it across to the new pin. Secondly this creates a new set of MKAnnotationViews, what happens to the old ones, are there and issues with memory leaks?

// Option2
//FOR: pinColor YES, animatesDrop YES, LABEL NO
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"annotation_ID"];
    if (pin == nil) {
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"annotation_ID"] autorelease];
    } else {
        pin.annotation = annotation;
    }
    pin.pinColor = MKPinAnnotationColorRed;
    pin.animatesDrop = YES;
    return pin;
}

EDIT: I have solved the missing title & subTitle by adding pin.canShowCallout = YES;

解决方案

MKPinAnnotationView is a subclass of MKAnnotationView.

MKAnnotationView is a generic annotation view for which you have to provide the image and animation if desired.

MKPinAnnotationView is a convenient subclass of MKAnnotationView which automatically provides a pin image in a selected color and an animation of the pin dropping onto the map. You set the animatesDrop property when creating the view in viewForAnnotation and it will handle the animation automatically from there.

If you don't implement viewForAnnotation, a standard red pin with no animation is displayed.

By the time didAddAnnotationViews is called, the automatic animation has already happened and setting that property there has no effect.

If however you want to create a custom animation different from the default drop animation that MKPinAnnotationView provides, you could do that in didAddAnnotationViews. The view will already be at its final destination point so you save that and then animate it from a different point to that destination.

If you're happy with the default drop animation that MKPinAnnotationView provides, you don't need to implement didAddAnnotationViews. That delegate method is more useful for doing other things that you might need to do when all the annotation views are actually in place.

For your pins to show the title, set canShowCallout to YES where you set animatesDrop.

Not sure what you mean by "this creates a new set of MKAnnotationViews". In the viewForAnnotation method, you are providing a view (MKPinAnnotationView) for the MKAnnotation object. They are not the same thing.

Also, the viewForAnnotation method works like the cellForRowAtIndexPath method for UITableView where annotation views can get recycled which is why it's important to set MKAnnotation-specific information in the view every time (such as the annotation property).

这篇关于MKMapView,animateDrop?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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