Prevent User interactions only on UIView and not on Subviews - Swift - Programmatically

39 views Asked by At

I have a UIView subclass that I created in this way:

class UpperView : UIView{

var currentColor : UIColor = .white

private var lineArray: [[CGPoint]] = [[CGPoint]]()

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    guard let touch = touches.first else { return }
    let firstPoint = touch.location(in: self)
    
    lineArray.append([CGPoint]())
    lineArray[lineArray.count - 1].append(firstPoint)
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    let currentPoint = touch.location(in: self)
    lineArray[lineArray.count - 1].append(currentPoint)
    setNeedsDisplay()
}

}

I want to prevent this UIView to receive events, so I searched online and I found out that I could set isUserInteractionEnabled = false

Using this method the view doesn't receive any event and so do all of its subviews.

This is the problem, how can I prevent the view to receive any touch event while still allowing its subviews to receive them?

0

There are 0 answers