3

我不想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];
4

5 回答 5

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;
    }
}
于 2013-07-14T02:29:22.337 回答
3

因此,您想要“在内容视图之外单击以关闭模式”之类的交易吗?

与其将点击手势添加到整个模态视图,不如使用一个背景视图来处理它,内容视图自然会拦截所有点击(如果它启用了用户交互),因此您基本上不必进行任何编码。

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];
于 2013-07-14T02:19:17.903 回答
1
  @objc func handleTap(_ sender: UITapGestureRecognizer){
    let point = sender.location(in: subview)
    if !(tapView.frame.contains(point)) {
        self.dismiss(animated: true, completion: nil)
    }

}

这里,tapview 是添加点击手势的视图,subview 是它的子视图

于 2019-06-13T11:39:30.563 回答
1

我只是检查触摸的视图是否是自我,否则它是一个子视图并且手势不应该开始:

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
    }
}
于 2016-03-29T18:16:58.660 回答
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)
    }
}
于 2021-07-08T07:47:19.123 回答