- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在学习如何在我初出茅庐的 iOS 应用程序中使用 MapKit。我将我的一些模型实体用作注释(将 <MKAnnotation>
协议(protocol)添加到它们的头文件中)。我还创建自定义 MKAnnotationViews 并设置 draggable
属性(property)YES
.
我的模型对象有一个 location
属性,这是一个 CLLocation*
.符合 <MKAnnotation>
协议(protocol),我向该对象添加了以下内容:
- (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;
}
所以,我有 4 个必需的方法。而且它们非常简单。当我在 MKAnnotationView
上阅读苹果文档时和 @draggable
属性,它表示如下:
Setting this property to YES makes an annotation draggable by the user. If YES, the associated annotation object must also implement the setCoordinate: method. The default value of this property is NO.
在其他地方,MKAnnotation
文档说:
Your implementation of this property must be key-value observing (KVO) compliant. For more information on how to implement support for KVO, see Key-Value Observing Programming Guide.
我已经阅读了那个(简短的)文档,但我完全不清楚我应该做什么来完成它,以便 coordinate
,这是我从我的 location
中得出的属性本身就是一个适当的属性。
但我有理由相信它无法正常工作。当我拖动图钉时,它会移动,但当我平移 map 时它不再重新定位。
更新
所以我尝试使用常用的 MKPinAnnotationView。为此,我简单地注释掉了我代表的 mapView:viewForAnnotation:
。方法。我发现这些默认情况下不可拖动。我添加了 mapView:didAddAnnotationViews:
给我的代表设置 draggable
添加到 YES
的 View 的属性.
这样配置后,Pin View (正如下面 John Estropia 所暗示的那样)似乎工作正常。我决定使用 mapView:annotationView:didChangeDragState:fromOldState:
delegate hook 以更仔细地了解正在发生的事情:
- (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]);
}
对于备用引脚,您将看到如下所示的日志输出:
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
这看起来很合乎逻辑。但是如果你切换到配置的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
它错过了两个过渡,从开始到拖动,从结束到无。
所以我开始怀疑我是否需要对属性做一些不同的事情。但我仍然对为什么这行不通感到沮丧。
更新 2
我创建了自己的 Annotation
对象站在我的模型对象之间,它可能有一个属性 coordinate
属性(property)。行为保持不变。这似乎与 MKAnnotationView
有关.
最佳答案
有很多关于如何使用委托(delegate)方法 mapView:viewForAnnotation:
的示例,这些示例展示了如何设置 MKAnnotationView
。但不那么明显的是,仅仅因为您将 MKAnnotationView
实例的 draggable
属性设置为 YES
,您仍然需要编写一些代码帮助它过渡一些州。 MapKit 会负责将实例的 dragState
移动到 MKAnnotationViewDragStateStarting
和 MKAnnotationViewDragStateEnding
,但它不会执行其他转换。您在关于子类化 MKAnnotationView
的文档注释中看到了这方面的提示,并且需要覆盖 `setDragState:animated:'
When the drag state changes to MKAnnotationViewDragStateStarting, set the state to MKAnnotationViewDragStateDragging. If you perform an animation to indicate the beginning of a drag, and the animated parameter is YES, perform that animation before changing the state.
When the state changes to either MKAnnotationViewDragStateCanceling or MKAnnotationViewDragStateEnding, set the state to MKAnnotationViewDragStateNone. If you perform an animation at the end of a drag, and the animated parameter is YES, you should perform that animation before changing the state.
在这种情况下,我没有子类化,但似乎 MKAnnotationView
仍然难以自行进行转换。所以你必须实现委托(delegate)的 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;}
}
}
这允许事情适本地完成,这样当您在拖动注释后平移 map 时,注释会随着平移移动。
关于iOS MapKit 拖动注释(MKAnnotationView)不再随 map 平移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21567082/
我正在尝试基于此示例实现 highcharts 平移: http://jsfiddle.net/HXUmK/5/ 但我希望能够用鼠标左键缩放并用鼠标右键平移。所以我修改了上面的代码并设法让它工作了一点
如何删除 BufferedImage 最左边垂直列的 50px,并将其复制到与原始 BufferedImage 大小相同的新 BufferedImage 中? class TestCopyImage
以下是关于如何绘制机器人的 ARM 和肩膀并通过一些用户输入旋转它们的代码(取自 http://www.glprogramming.com/red/chapter03.html ): glPushMa
我正在使用 Swift/SpriteKit 并在 SKScene 上创建了 map 的宽图像。我正在用代码完成这一切,并希望让用户拖动手指来平移这张超宽的 map 。可以将其想象为幻想世界的老式 RP
我正在将 opencv 与 C 一起使用,我正在尝试获取 2 个摄像头之间的外部参数(旋转和平移)。 有人告诉我可以使用棋盘格图案进行校准,但我找不到任何好的样本。我该怎么做? 编辑 给出的建议是用棋
我正在尝试在 android 图库小部件中缩放/平移图像。图像覆盖整个屏幕。虽然我可以缩放/平移图库中的图像,但我无法滑动到下一张/上一张图像。单个图像的缩放和平移效果很好。 我从 Hello And
当允许平移时,所有鼠标事件上的 JavaFX ScrollPane Pane : scrollPane.setPannable(true); 如何限制 ScrollPane 仅在鼠标中间事件上平移,
我一直试图弄清楚如何在 paperjs 中使用 onMouseDrag 和 onMouseDown 进行平移/缩放。 我看到的唯一引用是在 coffescript 中,并且不使用 paperjs 工具
我正在使用 d3 来渲染简化的甘特图,并使用 d3.behavior.zoom 进行平移和缩放。 x 比例是一个时间比例(稍微修改为以列为中心的日历天等)并且工作得很好,但是我在决定如何缩放/平移 y
我已经使用关键帧为HTML/CSS3创建了动画。 动画是一个图标,该图标应该围绕其中心点旋转和缩放。 该动画可在Chrome中运行,但在Safari中,直到动画结束,它才能正确转换。 这是-定位到 s
我有一个在登录事件后调用的 TranslationService,在这个服务中我想初始化 $translateProvider.translation 但这个对象似乎无法在 app.config(..
滚动和平移有什么区别? 平移是否被识别为拖动图像/背景的 Action ,而滚动仅在您使用滚动条时? 而且,拖动和平移有什么区别? 当我拖动google maps的 map 时,哪个术语合适,拖动还是
当用户按下鼠标并移动它时,我试图平移 Canvas ,但由于某种我看不到的原因,它似乎不起作用。有什么想法吗? canvas.addEventListener('mousedown', onM
我正在拼命寻找一种用 Pyglet 控制声音(左右)平衡的方法。 我知道 3D 定位系统,但我真正想要的只是控制平移(类似于 -1:left 1:right),而 3D 系统在这种情况下会适得其反(
考虑以下两组点。我想找到最佳的 2D 平移和旋转,以对齐数据集蓝色和数据集橙色之间的最大数量的点,如果到另一个数据集中最近邻居的距离小于阈值,则认为该点对齐。 我知道这与“迭代最近点”算法有关,但在这
我正在尝试包含 @bumbeishvili 的 Convex Hull 实现( https://bl.ocks.org/bumbeishvili/f027f1b6664d048e894d19e54fe
我正在尝试在平移和缩放时使可缩放/拖动矩形不超出 svg 边界。我尝试基于此 example 来实现它,但我似乎无法让它工作。我创建了 this jsfiddle只有可缩放和可拖动的矩形。再一次,我试
UIScrollView 有一个内置的行为“directionLockEnabled”。 启用后,平移将尝试锁定到水平或垂直方向。但是当用户积极尝试对角滚动时 - 它仍然允许对角滚动。 我想删除对角滚
我正在编写一个 GUI,它将执行一些图形平移/旋转等操作。 我的问题是,当我尝试翻译我的图形时, (a) 整个屏幕都会平移,而不是我的一小块绘制区域 (b) 旧油漆留在那里,留下一个大油漆 Blob
我最近一直在做一个项目,作为我的作业。 好吧,言归正传,我有一个 Scene2D 的 Screen 实现,我遇到的问题是我在小部件上的触摸事件不会在小部件上停止。 我的意思是,如果我弹出一个窗口,并尝
我是一名优秀的程序员,十分优秀!