Tra*_*ggs 3 properties key-value-observing mapkit ios ios7
我正在学习在我初出茅庐的iOS应用程序中使用MapKit.我正在使用我的一些模型实体作为注释(将<MKAnnotation>协议添加到其头文件中).我还创建自定义MKAnnotationViews并将draggable属性设置为YES.
我的模型对象有一个location属性,它是一个CLLocation*.为了符合<MKAnnotation>协议,我在该对象中添加了以下内容:
- (CLLocationCoordinate2D) coordinate {
return self.location.coordinate;
}
- (void) setCoordinate:(CLLocationCoordinate2D)newCoordinate {
CLLocation* newLocation = [[CLLocation alloc]
initWithCoordinate: newCoordinate
altitude: self.location.altitude
horizontalAccuracy: self.location.horizontalAccuracy
verticalAccuracy: self.location.verticalAccuracy
timestamp: nil];
self.location = newLocation;
}
- (NSString*) title {
return self.name;
}
- (NSString*) subtitle {
return self.serialID;
}
Run Code Online (Sandbox Code Playgroud)
所以,我有4个必需的方法.而且他们非常直截了当.当我阅读苹果文档MKAnnotationView和@draggable财产时,它说如下:
将此属性设置为YES会使用户可以拖动注释.如果是,则关联的注释对象还必须实现setCoordinate:方法.此属性的默认值为NO.
在其他地方,MKAnnotation文档说:
您对此属性的实现必须符合键值观察(KVO).有关如何实现对KVO的支持的更多信息,请参阅键值观察编程指南.
我已经读过那篇(简短的)文件了,我根本不清楚我应该做些什么来实现这一目标,以便coordinate我从我的location财产中得到的是一个适当的财产本身.
但我有理由相信它不能正常工作.当我拖动它时,它会移动,但是当我平移地图时它不再重新定位.
UPDATE
所以我试着玩MKPinAnnotationView股票.为此,我只是注释了我的委托mapView:viewForAnnotation:方法.我发现默认情况下这些都不可拖动.我添加了mapView:didAddAnnotationViews:我的委托以设置draggable添加的视图的属性YES.
一旦这样配置,如下面John Estropia暗示的Pin视图似乎工作正常.我决定使用mapView:annotationView:didChangeDragState:fromOldState:委托钩子来仔细看看发生了什么:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
NSArray* states = @[@"None", @"Starting", @"Dragging", @"Cancelling", @"Ending"];
NSLog(@"dragStateChangeFrom: %@ to: %@", states[oldState], states[newState]);
}
Run Code Online (Sandbox Code Playgroud)
对于库存引脚,可以看到如下所示的日志输出:
2014-02-05 09:07:45.924 myValve[1781:60b] dragStateChangeFrom: None to: Starting
2014-02-05 09:07:46.249 myValve[1781:60b] dragStateChangeFrom: Starting to: Dragging
2014-02-05 09:07:47.601 myValve[1781:60b] dragStateChangeFrom: Dragging to: Ending
2014-02-05 09:07:48.006 myValve[1781:60b] dragStateChangeFrom: Ending to: None
Run Code Online (Sandbox Code Playgroud)
这看起来很合乎逻辑.但是,如果切换到已配置MKAnnotationView,您将看到的输出如下所示:
2014-02-05 09:09:41.389 myValve[1791:60b] dragStateChangeFrom: None to: Starting
2014-02-05 09:09:45.451 myValve[1791:60b] dragStateChangeFrom: Starting to: Ending
Run Code Online (Sandbox Code Playgroud)
它错过了两个过渡,从开始到拖动,从结束到无.
所以我开始怀疑我需要做一些与属性不同的事情.但我仍然对为什么这不起作用感到沮丧.
更新2
我创建了自己的Annotation对象来站在我的模型对象之间,它可以有一个属性coordinate属性.行为保持不变.它似乎与之相关MKAnnotationView.
Tra*_*ggs 16
有很多关于如何使用委托方法mapView:viewForAnnotation:显示设置的示例MKAnnotationView.但是,显而易见的是,仅仅因为您将实例的draggable属性设置MKAnnotationView为YES,您仍然必须编写一些代码来帮助它转换某些状态.MapKit将负责将您的实例移动dragState到MKAnnotationViewDragStateStarting和MKAnnotationViewDragStateEnding,但它不会执行其他转换.您可以在关于子类化的文档说明中看到这一点,MKAnnotationView并且需要覆盖`setDragState:animated:'
当拖动状态更改为MKAnnotationViewDragStateStarting时,将状态设置为MKAnnotationViewDragStateDragging.如果执行动画以指示拖动的开始,并且动画参数为YES,则在更改状态之前执行该动画.
当状态更改为MKAnnotationViewDragStateCanceling或MKAnnotationViewDragStateEnding时,将状态设置为MKAnnotationViewDragStateNone.如果在拖动结束时执行动画,并且动画参数为YES,则应在更改状态之前执行该动画.
在这种情况下,我不是子类,但似乎MKAnnotationView仍然难以自己进行转换.所以你必须实现委托的mapView:annotationView:didChangeDragState:fromOldState:方法.例如
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState {
if (newState == MKAnnotationViewDragStateStarting) {
annotationView.dragState = MKAnnotationViewDragStateDragging;
}
else if (newState == MKAnnotationViewDragStateEnding || newState == MKAnnotationViewDragStateCanceling) {
annotationView.dragState = MKAnnotationViewDragStateNone;}
}
}
Run Code Online (Sandbox Code Playgroud)
这样可以适当地完成任务,因此在拖动注释后平移地图时,注释会随着平移移动.
| 归档时间: |
|
| 查看次数: |
4328 次 |
| 最近记录: |