简体   繁体   English

防止子视图上的UITapGestureRecognizer

[英]Prevent UITapGestureRecognizer on Subview

I don't want closeShareView being called when the user taps on contentView , only on modalView , how can I do that? 当用户点击contentView ,我不希望closeShareView ,仅在modalView ,我该怎么做?

UIWindow* mainWindow = (((AppDelegate *)[UIApplication sharedApplication].delegate).window);

// Modal view
UIView *modalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
modalView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];

// Content view (goes inside modalView)
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(modalView.center.x, modalView.center.y, 220, 220)];
[modalView addSubview:contentView];
[mainWindow addSubview:modalView];

// Tap gesture (added to modalView)
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeShareView:)];
gr.numberOfTapsRequired = 1;
[modalView addGestureRecognizer:gr];

Firstly, adhere to the UIGestureRecognizerDelegate protocol in your header file. 首先,在头文件中遵守UIGestureRecognizerDelegate协议。

Set the delegate of the UIGestureRecognizer to self . UIGestureRecognizer 的委托设置self

Then set the tag of your subview eg to 100 (but you should use a constant). 然后将子视图的标记设置为100(但您应该使用常量)。

Then use something like: 然后使用类似的东西:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:self.view];
    UIView *viewTouched = [self.view hitTest:point withEvent:nil];
    if (viewTouched.tag == 100) {
        // Don't BEGIN the gestureRecognizer
        return NO;
    } else {
        // Do BEGIN the gestureRecognizer
        return YES;
    }
}

So you want something like a "click outside the content view to dismiss the modal" kind of deal? 所以你想要一些像“在内容视图外点击以消除模态”这样的交易?

Instead of adding the tap gesture to the entire modal view, have a background view that handles that, and the content view will naturally intercept all taps (if it has user interaction enabled), so you basically don't have to do any coding. 不要将敲击手势添加到整个模态视图,而是使用处理该视图的背景视图,内容视图将自然拦截所有点击(如果它启用了用户交互),因此您基本上不必进行任何编码。

UIWindow* mainWindow = (((AppDelegate *)[UIApplication sharedApplication].delegate).window);

// Modal view
UIView *modalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Background tap view
UIView *backgroundView = [[UIView alloc] initWithFrame:modalView.bounds];
backgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.8];
[modalView addSubview:backgroundView];

// Content view (goes inside modalView)
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(modalView.center.x, modalView.center.y, 220, 220)];
[modalView addSubview:contentView];
[mainWindow addSubview:modalView];

// Tap gesture (added to backgroundView)
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeShareView:)];
gr.numberOfTapsRequired = 1;
[backgroundView addGestureRecognizer:gr];

I'm just checking to see if the touched view is self, else it's a subview and the gesture shouldn't begin: 我只是检查触摸的视图是否是自我,否则它是一个子视图,手势不应该开始:

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    let point = gestureRecognizer.locationInView(self.view)
    if let viewTouched = self.view.hitTest(point, withEvent: nil) where viewTouched != self.view {
        return false
    } else {
        return true
    }
}
  @objc func handleTap(_ sender: UITapGestureRecognizer){
    let point = sender.location(in: subview)
    if !(tapView.frame.contains(point)) {
        self.dismiss(animated: true, completion: nil)
    }

}

Here, tapview is the view on which the tap gesture is added and subview is it's subview 这里,tapview是添加了点击手势的视图,子视图是它的子视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MKAnnotationView的子视图上的UITapGestureRecognizer无法正常工作 - UITapGestureRecognizer on a subview of MKAnnotationView not working 将UITapGestureRecognizer添加到子视图 - Add a UITapGestureRecognizer to a subview UITapGestureRecognizer在动画子视图上没有响应 - UITapGestureRecognizer not responding on animated subview 将UITapGestureRecognizer添加到tableViewCell的子视图中 - Adding UITapGestureRecognizer to subview on tableViewCell 将UITapGestureRecognizer添加到UIPickerView的子视图中 - Add a UITapGestureRecognizer to a subview of UIPickerView 从UITapGestureRecognizer中排除子视图 - exclude subview from UITapGestureRecognizer 如何防止子视图上的拍子触发父视图上的UITapGestureRecognizer? - How do I prevent taps on a subview from triggering a UITapGestureRecognizer on a parent view? 在UIScrollView子视图中的UILabels上的UITapGestureRecognizer无法正常工作 - UITapGestureRecognizer on UILabels in subview of UIScrollView not working Swift 4 - 将UITapGestureRecognizer添加到子视图图像 - 不调用该方法 - Swift 4 - Adding UITapGestureRecognizer to a subview image - the method is not called MKAnnotationView子视图上的UITapGestureRecognizer无法正常工作。 迅速 - UITapGestureRecognizer on subview of MKAnnotationView not working. SWIFT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM