UIGestureRecognizer 用于 UIView 的一部分

发布于 2025-01-07 07:46:31 字数 241 浏览 0 评论 0原文

我在 iOS 应用程序中使用 UIGestureRecognizer,但遇到了一些问题。

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

我能做什么来解决这个问题?

谢谢。

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

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?

Thanks.

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

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

发布评论

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

评论(2

木緿 2025-01-14 07:46:32

你也可以做:

gestureRecognizer.delegate = self

某处。一般在 viewDidLoad() 上。然后你实现该方法:

 func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        let view = self.getTheViewDontWannaConsider() /* or whateva */

        let point = touch.location(in:view)
        if point.y >= 50 /* or whateva calc. you want */ {
           return false
        }
        return true
    }

Yo can also do:

gestureRecognizer.delegate = self

somewhere. generally on viewDidLoad(). then you implement the method:

 func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        let view = self.getTheViewDontWannaConsider() /* or whateva */

        let point = touch.location(in:view)
        if point.y >= 50 /* or whateva calc. you want */ {
           return false
        }
        return true
    }
喜你已久 2025-01-14 07:46:31

不要为手势识别器创建新视图。识别器实现了 locationInView: 方法。为包含敏感区域的视图进行设置。在handleGesture 上,对您关心的区域进行命中测试,如下所示:

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

1) 设置 mySensitiveRect

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

2) 创建手势识别器:

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");
    }
}

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

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) 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) Setup mySensitiveRect

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

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");
    }
}

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

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