如果涉及superview,则不会排除子视图点击的UIScrollView

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果涉及superview,则不会排除子视图点击的UIScrollView相关的知识,希望对你有一定的参考价值。

所以我试图创建一个界面,其中基本上有3级视图可能。

  1. UIView负责决定新细胞的元数据
  2. 细胞本身
  3. 后台UIScrollView

一切都是UIScrollView的孩子,但是1显示在2的顶部。

目标是所有单元格都是可拖动的,滚动视图是可滚动的。但是,当点击滚动视图时,应该触发创建新节点,从而显示视图1的外观,该视图将在按下提交按钮时消失。

我已经建立了几乎所有东西,除了我有一个巨大的问题。如果我点击一个单元格,我仍然会获得创建视图,这意味着TapGestureRecognizer不会排除子视图。所以这里大致是设置的:

class Node: UILabel {

    var recognizer:UIPanGestureRecognizer?
    init() {
        self.isExclusiveTouch = true
        self.isUserInteractionEnabled = true

        recognizer = UIPanGestureRecognizer(target: self, action: #selector(drag))
        self.addGestureRecognizer(recognizer!)
    }

    @objc
    func drag(rec: UIPanGestureRecognizer) {
        goToPoint(point: rec.location(in: self.superview!))
    }

    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        var shouldReceive = false
        if let clickedView = touch.view {
            if clickedView == self {
                shouldReceive = true;
            }
        }
        return shouldReceive
    }
}

class NodeCreator: UIView {

    var submit: UIButton = UIButton(type: UIButton.ButtonType.roundedRect)

    init() {
        super.init(frame: CGRect.zero)

        submit.setTitle("Submit", for: .normal)
        submit.translatesAutoresizingMaskIntoConstraints = false
        submit.addTarget(self, action: #selector(press), for: .touchUpInside)
        self.addSubview(submit)
    }

    @objc func press() {
        if let d = delegate {
            if let t = inputField.text {
                d.created(title: t, pt: storeP)
                self.removeFromSuperview()
            }
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
class TaskView: UIScrollView, CreationDelecate{

    func created(title: String, pt: CGPoint) {
        let temp = Node()
        view.addNode(temp, at: pt)
    }

    var tapRecognizer:UITapGestureRecognizer?

    var nodeSet:[Node] = []
    func addNode(_ n1: Node, at: CGPoint = CGPoint.zero) {
        nodeSet.append(n1)
        n1.goToPoint(point: at)
        self.addSubview(n1)
        self.bringSubviewToFront(n1)

        self.isScrollEnabled = true
        self.isUserInteractionEnabled = true
        self.isExclusiveTouch = false

        self.canCancelContentTouches = false

        if (tapRecognizer == nil) {
            tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tap))
            self.addGestureRecognizer(tapRecognizer!)
        }
    }

    @objc func tap(rec: UITapGestureRecognizer) {
        let pos = rec.location(in: self)
        let creator = NodeCreator(pt: pos)
        creator.delegate = self
        self.addSubview(creator)
    }

    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        var shouldReceive = false
        if let clickedView = touch.view {
            if clickedView == self {
                shouldReceive = true;
            }
        }
        return shouldReceive
    }
}

为什么当节点都是独占触摸时,TaskView仍然会接收到轻击手势。

答案

代码不起作用,因为您没有为手势识别器分配委托。委托函数在Node和TaskView类中实现。但你没有宣布课程为代表,即

class TaskView: UIScrollView, CreationDelecate, UIGestureRecognizerDelegate

将委托分配给手势识别器:

recognizer.delegate = self as! UIGestureRecognizerDelegate

应该管用

以上是关于如果涉及superview,则不会排除子视图点击的UIScrollView的主要内容,如果未能解决你的问题,请参考以下文章

当内部按钮触摸时,如何从 Superview 中删除以编程方式创建的子视图?

UIView 在其 Superview 内移动

从 SuperView 中删除子视图的问题 - Objective-C

将 superView 拖到某个阈值后启用滚动 UITableView

添加 SubView 后在 SuperView 中获取 Touches

在 Superview 中更改子视图的框架