Ios4 如何在MKMapView上捕捉点击手势
我试图在我的Ios4 如何在MKMapView上捕捉点击手势,ios4,mkmapview,uitapgesturerecognizer,mkoverlay,mkpinannotationview,Ios4,Mkmapview,Uitapgesturerecognizer,Mkoverlay,Mkpinannotationview,我试图在我的MKMapView上捕获点击事件,这样我就可以在用户点击的点上放置MKPinAnnotation。基本上,我有一张覆盖有mkoverlayView(显示建筑的覆盖图)的地图,我想在用户点击该覆盖图时,通过放置mkpinannotation并在标注中显示更多信息,为用户提供更多关于该覆盖图的信息。 多谢各位 您可以使用UIgestureRecognitor检测地图视图上的触摸 但是,我建议您不要只点击一次,而是点击两次(UIAppgestureRecognitizer)或长按(UILo
MKMapView
上捕获点击事件,这样我就可以在用户点击的点上放置MKPinAnnotation
。基本上,我有一张覆盖有mkoverlayView
(显示建筑的覆盖图)的地图,我想在用户点击该覆盖图时,通过放置mkpinannotation
并在标注中显示更多信息,为用户提供更多关于该覆盖图的信息。
多谢各位 您可以使用
UIgestureRecognitor
检测地图视图上的触摸
但是,我建议您不要只点击一次,而是点击两次(UIAppgestureRecognitizer
)或长按(UILongPressgestureRecognitizer
)。单次点击可能会干扰用户尝试单次点击pin或标注本身
在设置地图视图的位置(例如在viewDidLoad
中),将手势识别器连接到地图视图:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleGesture:)];
tgr.numberOfTapsRequired = 2;
tgr.numberOfTouchesRequired = 1;
[mapView addGestureRecognizer:tgr];
[tgr release];
或使用长按:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleGesture:)];
lpgr.minimumPressDuration = 2.0; //user must press for 2 seconds
[mapView addGestureRecognizer:lpgr];
[lpgr release];
在
手册测试中:
方法:
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate =
[mapView convertPoint:touchPoint toCoordinateFromView:mapView];
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = touchMapCoordinate;
pa.title = @"Hello";
[mapView addAnnotation:pa];
[pa release];
}
-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {
// This is important if you only want to receive one tap and hold event
if (sender.state == UIGestureRecognizerStateEnded)
{NSLog(@"Released!");
[self.mapView removeGestureRecognizer:sender];
}
else
{
// Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
CGPoint point = [sender locationInView:self.mapView];
CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
// Then all you have to do is create the annotation and add it to the map
MyLocation *dropPin = [[MyLocation alloc] init];
dropPin.latitude = [NSNumber numberWithDouble:locCoord.latitude];
dropPin.longitude = [NSNumber numberWithDouble:locCoord.longitude];
// [self.mapView addAnnotation:dropPin];
[mapAnnotations addObject:dropPin];
[dropPin release];
NSLog(@"Hold!!");
NSLog(@"Count: %d", [mapAnnotations count]);
}
}
我在viewDidLoad:
中设置了长按(ui长按手势识别器
),但它只检测到第一次触摸时的一次触摸
在哪里可以设置长按以检测所有触摸?(这意味着每次等待用户触摸屏幕以按下pin时,地图已准备就绪)
viewDidLoad:
方法
- (void)viewDidLoad {
[super viewDidLoad];mapView.mapType = MKMapTypeStandard;
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
[self.mapView addGestureRecognizer:longPressGesture];
[longPressGesture release];
mapAnnotations = [[NSMutableArray alloc] init];
MyLocation *location = [[MyLocation alloc] init];
[mapAnnotations addObject:location];
[self gotoLocation];
[self.mapView addAnnotations:self.mapAnnotations];
}
以及手动长按手势
方法:
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate =
[mapView convertPoint:touchPoint toCoordinateFromView:mapView];
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = touchMapCoordinate;
pa.title = @"Hello";
[mapView addAnnotation:pa];
[pa release];
}
-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {
// This is important if you only want to receive one tap and hold event
if (sender.state == UIGestureRecognizerStateEnded)
{NSLog(@"Released!");
[self.mapView removeGestureRecognizer:sender];
}
else
{
// Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
CGPoint point = [sender locationInView:self.mapView];
CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
// Then all you have to do is create the annotation and add it to the map
MyLocation *dropPin = [[MyLocation alloc] init];
dropPin.latitude = [NSNumber numberWithDouble:locCoord.latitude];
dropPin.longitude = [NSNumber numberWithDouble:locCoord.longitude];
// [self.mapView addAnnotation:dropPin];
[mapAnnotations addObject:dropPin];
[dropPin release];
NSLog(@"Hold!!");
NSLog(@"Count: %d", [mapAnnotations count]);
}
}
如果您想在地图视图中使用单击/点击,下面是我正在使用的代码片段。(可可和斯威夫特) 在手势委托方法中,选择双击手势的简单测试
func gestureRecognizer(gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: NSGestureRecognizer) -> Bool {
let other = otherGestureRecognizer as? NSClickGestureRecognizer
if (other?.numberOfClicksRequired > 1) {
return true; // allows double click
}
return false
}
如果希望地图处于各种“状态”(其中一种状态允许单次点击/点击),也可以在其他代理方法中过滤手势。出于某种原因,UIGestureRecognitor在Swift中不适用于我。当我使用UIgestureRecognitor方式时。当我使用touchesEnded方法时,它返回一个MKNewAnnotationContainerView。似乎此MKNewAnnotationContainerView阻止了我的MKMapView。幸运的是,它是MKMapView的一个子视图。因此,我循环使用MKNewAnnotationContainerView的SuperView,直到self.view,以获得MKMapView。我通过轻敲来锁定地图视图 Swift 4.1
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let t = touches.first
print(t?.location(in: self.view) as Any)
print(t?.view?.superview?.superview.self as Any)
print(mapView.self as Any)
var tempView = t?.view
while tempView != self.view {
if tempView != mapView {
tempView = tempView?.superview!
}else if tempView == mapView{
break
}
}
let convertedCoor = mapView.convert((t?.location(in: mapView))!, toCoordinateFrom: mapView)
let pin = MKPointAnnotation()
pin.coordinate = convertedCoor
mapView.addAnnotation(pin)
}
override func touchesend(touch:Set,带有事件:UIEvent?){
让t=先触摸
打印(t?位置(在:self.view中)如有)
打印(t?.view?.superview?.superview.self,如有)
打印(mapView.self,如有)
var tempView=t?.view
而tempView!=self.view{
如果tempView!=mapView{
tempView=tempView?.superview!
}如果tempView==mapView,则为else{
打破
}
}
让convertedCoor=mapView.convert((t?.location(in:mapView))!,到坐标from:mapView)
设pin=MKPointAnnotation()
pin.coordinate=convertedCoor
mapView.addAnnotation(pin)
}
谢谢您的建议,一旦我的建议生效,我会尽快回复。我确实试过一次点击,但在那之后,我无法显示我的标注。看起来我需要使用LongPressureGestureUtiapgestureRecognitizer在iOS 6的MKMapView上不再识别。它在iOS 5中工作。关于这个问题有什么想法吗?@phix23,尝试使用GestureRecognitor实现应同时识别,然后从那里返回YES
。需要执行tgr.delegate=self
在为要调用的ShoudRecognite委托方法添加GR之前。对“合法”链接有何想法(不确定在提出此问题时是否存在此链接)?我想让它正常运行,我想做一些比迭代地图视图的子视图更聪明的事情,看看触摸是否在标签内或是其他什么东西。也添加UIPangestureRecognitor来处理缩放(收缩)和移动(平移)