检测从iPhone MapView开始的捏合手势

时间:2012-01-03 06:04:28

标签: iphone ios xcode

我们正在为iPhone开发基于地图的应用程序。我们需要在MapView上检测拖动和捏合手势的开始。

我们可以使用regionWillChange委托方法检测拖动的开始。我们无法检测到捏手势的开始。

我们正在寻找一种检测捏手势开始的方法。可以在MapView上使用吗?

先谢谢。

编辑: 我们尝试使用以下代码,但它没有用。

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(checkpinch)]; 
[self.mapView addGestureRecognizer:pinch]; 
[pinch release];

双击手势识别器正在使用类似代码处理MapView。

UITapGestureRecognizer *Tap= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checktap)]; 
[self.mapView addGestureRecognizer:Tap]; 
[Tap release];

3 个答案:

答案 0 :(得分:4)

完美而简单的解决方案。

首先在.h文件中添加UIGestureRecognizerDelegate 在.m文件中添加以下两种方法

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return YES;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{
    return  YES;
}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

{
    return  YES;
}

在viewdidload中进行以下更改

[self.view insertSubview:mapView atIndex:0];

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(yourmethod)];
[pinch setDelegate:self];
[pinch setDelaysTouchesBegan:YES];
[self.mapView addGestureRecognizer:pinch];
[pinch release];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(youemethod)];
[tap setDelegate:self];
[self.mapView addGestureRecognizer:tap];
[tap release];

答案 1 :(得分:0)

您可以参考苹果示例代码:

http://developer.apple.com/library/ios/#samplecode/WorldCities/Introduction/Intro.html

该代码说明了MKMapView上的拖动和缩放操作

答案 2 :(得分:0)

您可以在MapView中添加手势识别器,并连接到IBAction以处理特定的访客。 在IBAction中,您可以检测特定客人的开始/结束。

示例:

- (IBAction)handlePanGuesture:(UIGestureRecognizer *)recogonizer {
    if (recogonizer.state == UIGestureRecognizerStateBegan) {
        // do stuff
    } 
  }