gpt4 book ai didi

ios - View 可见部分的触摸事件 ios swift 4

转载 作者:可可西里 更新时间:2023-11-01 01:57:01 24 4
gpt4 key购买 nike

我正在使用 Swift4 中的 Photo Collage,我使用 UIBezierPath 创建了拼贴,如下所示

enter image description here

我在 Storyboard 中有 5 个 ScrollView ,Scrollviews 的顺序如下所示

enter image description here

我正在使用以下代码创建Shapes:

    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

现在的问题是我无法获取 Scrollviews 的触摸事件,因为 Scroll5 位于顶部。我想接触重叠 View ,例如 Scroll1Scroll2 等等。简而言之,我需要在 View 可见的区域部分的特定 View 的触摸事件。

请参阅下面我想要触摸查看的图像。

enter image description here

我怎样才能接触到Overlapped Views

请帮忙!

最佳答案

你必须确定你先将scrollview的superView设置为isUserInterfaceEnabled=true

获取重叠 View 触摸事件:

这是我的代码:

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
}

hitTest 函数决定了您触摸的是哪个 View ,因此您只需返回重叠的 View 即可。

关于ios - View 可见部分的触摸事件 ios swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51764755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com