Reputation: 97
I have two UIViews A and B. Both of these views are placed one on another. Suppose View A is placed above B. So when I tap on View A it is consuming all tap actions. But I dont want that action to be consumed till View A only. I want to get actions on View B where it was tapped.
All my view has userInteractionEnabled = true
class PassthroughView : UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
{
let view = super.hitTest(point, with: event)
return view == self ? nil : view
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
print("Tap Called")
return false
}
}
I tried these codes after see some solutions but doesnt seems to be working.
Also View A has swipe gesture to work.
_V_i_e_w_B___
| ____
| |View A
| |
| |
| |
| |
|__|__
|_____
Upvotes: 0
Views: 335
Reputation: 15768
In UIView subclass override hitTest method only
class PassthroughView : UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
{
let view = super.hitTest(point, with: event)
return view == self ? nil : view
}
}
And create view A with the subclass and another view B with UIView class.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let b = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
b.backgroundColor = .gray
b.center = view.center
view.addSubview(b)
let a = PassthroughView(frame: CGRect(x: 0, y: 0, width: 275, height: 275))
a.backgroundColor = .lightGray
a.center = view.center
view.addSubview(a)
}
}
Upvotes: 1
Reputation: 1133
Whenever View A is placed above View B change the userInteractionEnabled to false for View A and your tap will be passed to View B.
Upvotes: 0