UIGestureRecognizer用于部分UIView [英] UIGestureRecognizer for part of a UIView

查看:109
本文介绍了UIGestureRecognizer用于部分UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的iOS应用程序中使用UIGestureRecognizer而且我遇到了一些问题。

I'm using UIGestureRecognizer in my iOS application and I'm having some issues.

我只希望手势在视图的某个区域内工作,所以我用一个特定的框架创建了一个新的UIView并将其添加到根视图中。手势在这方面工作得很好,但现在唯一的问题是我无法点击新视图下面/后面的东西(根视图上的对象)。如果我将userInteractionEnabled设置为NO,它会断开手势,因此这不是一个选项。

I only want the gestures to work in a certain area of the view, so I made a new UIView with a specific frame and added it to the root view. The gestures are working fine with this, but the only issue now is that I can't click the stuff that is under/behind that new view (the objects that are on the root view). If I set userInteractionEnabled to NO, it breaks the gestures so that is not an option.

我该怎么做才能解决这个问题?

What can I do to fix that?

谢谢。

推荐答案

不要为手势识别器创建新视图。识别器实现了locationInView:方法。将其设置为包含敏感区域的视图。在handleGesture上,点击测试您关注的区域,如下所示:

Don't create a new view for your gesture recognizer. The recognizer implements a locationInView: method. Set it up for the view that contains the sensitive region. On the handleGesture, hit-test the region you care about like this:

0)在包含您关注的区域的视图上执行所有这些操作。不要仅为手势识别器添加特殊视图。

0) Do all this on the view that contains the region you care about. Don't add a special view just for the gesture recognizer.

1)设置mySensitiveRect

1) Setup mySensitiveRect

@property (assign, nonatomic) CGRect mySensitiveRect;
@synthesize mySensitiveRect=_mySensitiveRect;
self.mySensitiveRect = CGRectMake(0.0, 240.0, 320.0, 240.0);

2)创建你的gestureRecognizer:

2) Create your gestureRecognizer:

gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self.view addGestureRecognizer:gr];
// if not using ARC, you should [gr release];
// mySensitiveRect coords are in the coordinate system of self.view


- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:self.view];
    if (CGRectContainsPoint(mySensitiveRect, p)) {
        NSLog(@"got a tap in the region i care about");
    } else {
        NSLog(@"got a tap, but not where i need it");
    }
}

敏感的rect应该在myView的坐标系中初始化,您附加识别器的相同视图。

The sensitive rect should be initialized in myView's coordinate system, the same view to which you attach the recognizer.

这篇关于UIGestureRecognizer用于部分UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆