0

我有一个图像,我想设置它来响应几个不同的手势响应器。因此,例如,如果触摸图片的一部分,我希望调用一个选择器,并为图片的不同部分调用另一个选择器。

我查看了UIGestureRecognizerUITapGestureRecognizer类,但找不到指定要与它们关联的图像区域的方法。这在iOS中完全可能吗?如果是这样,我应该考虑使用哪些类?

4

2 回答 2

3

最简单的解决方案是在图像上放置不可见的视图并在其上放置手势识别器。

如果这不可行,您将不得不查看手势识别器的点击处理程序中的 locationInView,并根据用户点击的位置确定您想要做什么。

于 2012-01-05T17:30:16.373 回答
2

使用该locationInView:属性来确定您的点击发生的位置,然后有条件地调用一个方法。您可以通过设置一些CGRect与您的命中区域相对应的 s 来做到这一点。然后使用该CGRectContainsPoint()函数来确定水龙头是否落在其中一个命中区域。

您的点击手势识别器操作可能如下所示:

- (void)tapGestureRecognized:(UIGestureRecognizer *)recognizer
{
    // Specify some CGRects that will be hit areas
    CGRect firstHitArea = CGRectMake(10.0f, 10.0f, 44.0f, 44.0f);
    CGRect secondHitArea = CGRectMake(64.0f, 10.0f, 44.0f, 44.0f)

    // Get the location of the touch in the view's coordinate space
    CGPoint touchLocation = [recognizer locationInView:recognizer.view];

    if (CGRectContainsPoint(firstHitArea, touchLocation))
    {
        [self firstMethod];
    }
    else if (CGRectContainsPoint(secondHitArea, touchLocation))
    {
        [self secondMethod];
    }
}
于 2012-01-05T18:21:11.923 回答