[英]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.