ArcGIS for iOS 开发系列(5) – 基础篇-手势和事件响应
2012-10-11 09:32
579 查看
MapView默认支持以下手势操作及对应触发的通知:
表3-2-1 支持的手势及对应通知
*地图旋转前提是MapView的allowRotationByPinching属性为YES。
在viewDidLoad方法中先注册“MapDidEndPanning”和“MapDidEndZooming”这两个通知。
// register for "MapDidEndPanning"notifications
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(respondToEnvChange:)name:@"MapDidEndPanning"object:nil];
// register for "MapDidEndZooming"notifications
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(respondToEnvChange:)name:@"MapDidEndZooming"object:nil];
然后创建一个“respondToEnvChange”方法接收通知,如下:
// The method that should be called when thenotification arises
- (void)respondToEnvChange:(NSNotification*) notification {
//create the string containing the new mapextent NSString*
NSString* theString = [[NSStringalloc]initWithFormat:@"xmin
= %f,\nymin = %f,\nxmax = %f,\nymax= %f",
self.mapView.visibleArea.envelope.xmin,
self.mapView.visibleArea.envelope.ymin,
self.mapView.visibleArea.envelope.xmax,
self.mapView.visibleArea.envelope.ymax];
//display the new map extent in a simple alert
UIAlertView* alertView = [[UIAlertViewalloc] initWithTitle:@"mapViewDidEndPanning"
message:theStringdelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];
[alertView show];
}
此外还有两个通知:“MapViewDidLoad”和“MapTimeExtentChanged”,用法同上。
图3-2-1 Delegation设计模式
下面先简单介绍下MapView中主要的三个委托,分别是:
图层委托(Layer Delegate)
图层委托中封装了地图与图层加载相关的事件响应,包括:
图3-2-2 图层委托
触摸
4000
委托(Touch Delegate)
触摸委托中封装了与触摸动作相关的事件响应,包括:
图3-2-3 触摸委托
弹出框委托(CalloutDelegate)
弹出框委托中封装了与弹出框相关的事件响应,包括:
图3-2-4 弹出框委托
委托的用法也非常简单,以图层委托为例,先在头文件中引用<AGSMapViewLayerDelegate>,然后在类文件中实现该委托中对应的方法:
//1. 引用委托
@interface ViewController: UIViewController <AGSMapViewLayerDelegate>
...
@end
@implementation ViewController
//2. 实现委托对应的方法
- (void)mapView:(AGSMapView*) mapView failedLoadingLayerForLayerView:(UIView *) layerView baseLayer:(BOOL)baseLayer withError:(NSError *) error {
//出错了做处理
//A. 删除出错图层
if(!baseLayer)
[self.mapViewremoveMapLayerWithName:layerView.name];
//B. 然后重新加载图层
AGSTiledMapServiceLayer* tiledLyr = (AGSTiledMapServiceLayer*)laverView.agsLayer;
[tiledLyr resubmitWithURL:url credential:cred];
}
//3. 设定图层委托
- (void)viewDidLoad {
...
self.mapView.layerDelegate= self;
}
除了这里给出的响应方法,开发者也可以自定义委托来对地图事件或动作做出响应。
表3-2-1 支持的手势及对应通知
手势 | 地图响应 | 触发的通知 |
轻划(Swipe) | 平移 | MapDidEndPanning |
撑开(Pinch-Out) | 放大 | MapDidEndZooming |
捏合(Pinch-In) | 缩小 | MapDidEndZooming |
双击(Double Tap) | 放大 | MapDidEndZooming |
两点点击(Two finger Tap) | 缩小 | MapDidEndZooming |
两点转动(Two finger Twist)* | 旋转 | |
1.地图通知
地图平移和缩放动作的通知非常有用,帮助开发者对地图范围变化做出响应,其调用方法如下:在viewDidLoad方法中先注册“MapDidEndPanning”和“MapDidEndZooming”这两个通知。
// register for "MapDidEndPanning"notifications
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(respondToEnvChange:)name:@"MapDidEndPanning"object:nil];
// register for "MapDidEndZooming"notifications
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(respondToEnvChange:)name:@"MapDidEndZooming"object:nil];
然后创建一个“respondToEnvChange”方法接收通知,如下:
// The method that should be called when thenotification arises
- (void)respondToEnvChange:(NSNotification*) notification {
//create the string containing the new mapextent NSString*
NSString* theString = [[NSStringalloc]initWithFormat:@"xmin
= %f,\nymin = %f,\nxmax = %f,\nymax= %f",
self.mapView.visibleArea.envelope.xmin,
self.mapView.visibleArea.envelope.ymin,
self.mapView.visibleArea.envelope.xmax,
self.mapView.visibleArea.envelope.ymax];
//display the new map extent in a simple alert
UIAlertView* alertView = [[UIAlertViewalloc] initWithTitle:@"mapViewDidEndPanning"
message:theStringdelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];
[alertView show];
}
此外还有两个通知:“MapViewDidLoad”和“MapTimeExtentChanged”,用法同上。
2.事件响应
其他地图事件响应的设计遵循Delegation模式,调用了MapView并想获得地图事件响应的类必须先引用对应的委托,这样特定事件触发后会通知这个类进行处理。图3-2-1 Delegation设计模式
下面先简单介绍下MapView中主要的三个委托,分别是:
图层委托(Layer Delegate)
图层委托中封装了地图与图层加载相关的事件响应,包括:
图3-2-2 图层委托
触摸
4000
委托(Touch Delegate)
触摸委托中封装了与触摸动作相关的事件响应,包括:
图3-2-3 触摸委托
弹出框委托(CalloutDelegate)
弹出框委托中封装了与弹出框相关的事件响应,包括:
图3-2-4 弹出框委托
委托的用法也非常简单,以图层委托为例,先在头文件中引用<AGSMapViewLayerDelegate>,然后在类文件中实现该委托中对应的方法:
//1. 引用委托
@interface ViewController: UIViewController <AGSMapViewLayerDelegate>
...
@end
@implementation ViewController
//2. 实现委托对应的方法
- (void)mapView:(AGSMapView*) mapView failedLoadingLayerForLayerView:(UIView *) layerView baseLayer:(BOOL)baseLayer withError:(NSError *) error {
//出错了做处理
//A. 删除出错图层
if(!baseLayer)
[self.mapViewremoveMapLayerWithName:layerView.name];
//B. 然后重新加载图层
AGSTiledMapServiceLayer* tiledLyr = (AGSTiledMapServiceLayer*)laverView.agsLayer;
[tiledLyr resubmitWithURL:url credential:cred];
}
//3. 设定图层委托
- (void)viewDidLoad {
...
self.mapView.layerDelegate= self;
}
除了这里给出的响应方法,开发者也可以自定义委托来对地图事件或动作做出响应。
相关文章推荐
- ArcGIS for iOS 开发系列(10) – 基础篇-空间要素-符号渲染和属性信息
- ArcGIS for iOS 开发系列(4) – 基础篇-地图组件
- 【教程连载】ArcGIS for iOS 开发系列(8) – 基础篇-空间要素-符号渲染和属性信息
- 【教程连载】ArcGIS for iOS 开发系列(7) – 基础篇-空间要素-几何形状
- ArcGIS for iOS 开发系列(9) – 基础篇-空间要素-几何形状
- 【教程连载】ArcGIS for iOS 开发系列(6) – 基础篇-图层-动态图层
- ArcGIS for iOS 开发系列(7) – 基础篇-图层-动态图层
- 【教程连载】ArcGIS for iOS 开发系列(5) – 基础篇-图层-静态图层
- ArcGIS for iOS 开发系列(6) – 基础篇-图层-静态图层
- 【教程连载】ArcGIS for iOS 开发系列(4) – 基础篇-地图组件
- ArcGIS for iOS 开发系列(3) – 基础篇-“Hello World Map”
- 【教程连载】ArcGIS for iOS 开发系列(3) – 基础篇-“Hello World Map”
- ArcGIS for iOS 开发系列(8) – 基础篇-图例、环绕和时态
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
- ArcGIS Runtime SDK for iOS开发系列教程(6)——Tasks使用的一般流程
- 【教程连载】ArcGIS for iOS 开发系列(1) – 基本概念
- ArcGIS Runtime SDK for iOS开发系列教程(2)——MapViewDemo解析
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控