gpt4 book ai didi

iphone - 如何长按并抬起 MKAnnotationView

转载 作者:行者123 更新时间:2023-12-03 20:51:51 25 4
gpt4 key购买 nike

我在MKMapView上添加了一个MKAnnotationView,当我长按它时,我希望它可以被抬起并移动到另一个位置。

如何做?特别感谢!

最佳答案

适用于 iOS 4.0 或更高版本

摘自 Apple 位置感知指南 > Marking Your Annotation View as Draggable :

To implement minimal support for dragging, you must do the following:

  • In your annotation objects, implement the setCoordinate: method to allow the map view to update the annotation’s coordinate point.

  • When creating your annotation view, set its draggable property to YES.

实现设置坐标:

#import <MapKit/MapKit.h>
@interface mapAnnotation : NSObject<MKAnnotation> {
double latitude, longitude;
}
@end

@implementation mapAnnotation
- (void)setCoordinate:(CLLocationCoordinate2D)coordinate{
latitude = coordinate.latitude;
longitude = coordinate.longitude;
}

-(CLLocationCoordinate2D)coordinate {
CLLocationCoordinate2D coordinate = {latitude,longitude};
return coordinate;
}
// ...
@end

在 View Controller 中创建注释 View :

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString * const identifier = "identifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (pinView ==nil) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];
pinView.animatesDrop = YES;
pinView.draggable = YES;
}
return pinView;
}

适用于任何 iOS 版本

跟踪用户触摸并相应地移动图钉 View 。看一下本页底部的DDAnnotationView:http://digdog.tumblr.com/post/252784277/mapkit-annotation-drag-and-drop-with-callout-info

当用户停止拖动时,将 View 位置转换为 map 坐标并更改注释坐标。为图钉添加一些动画。

这是一个工作示例:https://github.com/digdog/MapKitDragAndDrop尝试将 DDAnnotationView 和 DDAnnotation 类添加到您的项目中,并使用下面的 MKMapViewDelegate 方法 this line .

关于iphone - 如何长按并抬起 MKAnnotationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13033310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com