很简单的,直接贴代码!!
- (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状态都会被调用一次。把这两个状态区分开就可以。