很简单的,直接贴代码!!
- (void)viewDidLoad
{
[super viewDidLoad];
[self addUserLongPressAction];
// Do any additional setup after loading the view from its nib.
}
-(void)addUserLongPressAction {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addAnnotationForMap:)];
[longPress setMinimumPressDuration:0.8];
[myMapView addGestureRecognizer:longPress];
[longPress release];
}
- (void) addAnnotationForMap:(UILongPressGestureRecognizer*)press {
//解决响应两次的问题
if (press.state == UIGestureRecognizerStateEnded) {
return;
} else if (press.state == UIGestureRecognizerStateBegan) {
CGPoint point = [press locationInView:self.view];
CLLocationCoordinate2D coo = [myMapView convertPoint:point toCoordinateFromView:myMapView];
NSLog(@"经纬度:%lf, %lf", coo.longitude, coo.latitude);
NSLog(@"调用一次");
}
return;
}长按手势的响应有状态之分,action在 长按手势的 began和ended状态都会被调用一次。把这两个状态区分开就可以。
本文介绍了一种在iOS应用中实现地图长按手势的方法。通过使用`UILongPressGestureRecognizer`,该方法能够检测用户的长按时长,并在长按结束后获取地图上的坐标。文章详细展示了如何设置长按时长阈值及如何获取地图上长按位置的具体坐标。
2905

被折叠的 条评论
为什么被折叠?



