单个视图的多个手势响应器

发布于 2024-12-24 19:56:24 字数 215 浏览 1 评论 0原文

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

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

I have an image that I would like to set up to respond to several different gesture responders. So for example, if one part of the picture is touched I would like one selector to be called, and another selector for a different part of the picture.

I looked at the UIGestureRecognizer and UITapGestureRecognizer classes, but I couldn't find a way to specify the image zones to be associated with them. Is this at all possible in iOS? And if so what classes should I look into using?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎会甘心 2024-12-31 19:56:24

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

如果这不可行,您必须查看手势识别器的点击处理程序中的 locationInView ,并根据用户点击的位置找出您想要执行的操作。

The easiest solution is to lay invisible views over the image and put the gesture recognizers on them.

If that's not feasible you'll have to look at the locationInView in the gesture recognizer's tap handler and figure out what you want to do based on where the user tapped.

水染的天色ゝ 2024-12-31 19:56:24

使用 locationInView: 属性确定点击发生的位置,然后有条件地调用方法。您可以通过设置一些与您的点击区域相对应的 CGRect 来做到这一点。然后使用 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];
    }
}

Use the locationInView: property to determine where your tap occurred and then conditionally invoke a method. You can do this by setting up some CGRects that correspond to your hit areas. Then use the CGRectContainsPoint() function to determine if the tap landed in one of the hit areas.

Your tap gesture recognizer action may look something like this:

- (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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文