当用户点击closeShareView
时,我不希望contentView
被调用,仅在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];
答案 0 :(得分:3)
首先,遵循头文件中的UIGestureRecognizerDelegate
协议。
将UIGestureRecognizer
的代理设置为self
。
然后设置子视图的标记,例如到100(但你应该使用常数)。
然后使用类似的东西:
- (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;
}
}
答案 1 :(得分:1)
所以你想要一些像“在内容视图外点击以解除模态”这样的交易?
不是将水龙头手势添加到整个模态视图,而是有一个处理它的背景视图,内容视图会自然拦截所有水龙头(如果它启用了用户交互),所以你基本上不必做任何编码。
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];
答案 2 :(得分:0)
我只是检查被触摸的视图是否是自我,否则它是一个子视图,手势不应该开始:
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
}
}
答案 3 :(得分:0)
@objc func handleTap(_ sender: UITapGestureRecognizer){
let point = sender.location(in: subview)
if !(tapView.frame.contains(point)) {
self.dismiss(animated: true, completion: nil)
}
}
在这里,tapview是添加了轻击手势的视图,子视图是其子视图
答案 4 :(得分:0)
我上面有一个 backgroundView
和一个 tableView
:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(backgroundTapped))
tapGestureRecognizer.delegate = self
backgroundView.addGestureRecognizer(tapGestureRecognizer)
// MARK: - UIGestureRecognizerDelegate
extension PhoneListViewController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
let point = gestureRecognizer.location(in: self.tableView)
return !self.tableView.point(inside: point, with: nil)
}
}