代码之家  ›  专栏  ›  技术社区  ›  Khushbu Desai

ios swift 4视图可见部分的触摸事件

  •  2
  • Khushbu Desai  · 技术社区  · 6 年前

    我的工作是 照片拼贴 它在 Swift4 ,我用 UIBezierPath 如下所示

    enter image description here

    我有5个滚动视图 Storyboard 以及 Scrollviews 如下所示

    enter image description here

    使用我正在创建的以下代码 形状 :

        var path1 = UIBezierPath()
        path1.move(to: CGPoint(x: 0, y: 0))
        path1.addLine(to: CGPoint(x: superView.frame.width / 2, y: 0))
        path1.addLine(to: CGPoint(x: 0, y: superView.frame.width / 2))
        path1.addLine(to: CGPoint(x: 0, y: 0))
    
        var borderPathRef1 = path1.cgPath
    
        var borderShapeLayer1 = CAShapeLayer()
        borderShapeLayer1.path = borderPathRef1
    
        scroll1.layer.mask = borderShapeLayer1
        scroll1.layer.masksToBounds = true
    
    
        var path2 = UIBezierPath()
        path2.move(to: CGPoint(x: 0, y: 0))
        path2.addLine(to: CGPoint(x: superView.frame.width / 2, y: 0))
        path2.addLine(to: CGPoint(x: superView.frame.width / 2, y: superView.frame.width / 2))
        path2.addLine(to: CGPoint(x: 0, y: 0))
    
        var borderPathRef2 = path2.cgPath
    
        var borderShapeLayer2 = CAShapeLayer()
        borderShapeLayer2.path = borderPathRef2
    
        scroll2.layer.mask = borderShapeLayer2
        scroll2.layer.masksToBounds = true
    

    现在的问题是我无法得到 滚动视图 作为 Scroll5 在上面。我想接触重叠的视图,比如 Scroll1 , Scroll2 等等。总之我需要 在视图可见的区域部分上的特定视图的触摸事件。

    请看下面的图片,我想触摸查看。

    enter image description here

    我怎样才能接触到 Overlapped Views ?

    请帮忙!

    1 回复  |  直到 6 年前
        1
  •  1
  •   Versus    6 年前

    必须确保将scrollview的superView设置为 isUserInterfaceEnabled=true 第一。

    要获取重叠视图触摸事件:

    这是我的代码:

    class CustomView: UIView {
    let view1: UIView
    let view2: UIView
    let view3: UIView
    let view4: UIView
    let view5: UIView
    
    override init(frame: CGRect) {
        view1 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        view1.backgroundColor = UIColor.black
        view1.isUserInteractionEnabled = true
        view2 = UIView(frame: CGRect(x: 100, y: 0, width: 100, height: 100))
        view2.backgroundColor = UIColor.orange
        view2.isUserInteractionEnabled = true
        view3 = UIView(frame: CGRect(x: 0, y: 100, width: 100, height: 100))
        view3.backgroundColor = UIColor.blue
        view3.isUserInteractionEnabled = true
        view4 = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        view4.backgroundColor = UIColor.brown
        view4.isUserInteractionEnabled = true
        view5 = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
        view5.tag = 121
        view5.backgroundColor = UIColor.red
        super.init(frame: frame)
        self.addSubview(view1)
        self.addSubview(view2)
        self.addSubview(view3)
        self.addSubview(view4)
        self.addSubview(view5)
        view1.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(touchAction)))
    }
    
    @objc func touchAction () {
        print("----------- touch view 1")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if (view1.point(inside: self.convert(point, to: view1), with: event)) {
            return view1
        }
        return self
    }
    

    }

    功能 碰撞试验 决定你正在触摸哪个视图,所以你只需要返回重叠视图。