I have a custom UIView
that can be simplified down to:
class Node: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
let tapGR = UITapGestureRecognizer(target: self, action: "createChildNode")
addGestureRecognizer(tapGR)
userInteractionsEnabled = true
}
/* ... */
func createChildNode() {
let childNode = Node(frame: self.bounds.offset(dx: 100, dy: 100))
self.addSubview(childNode)
}
}
The first (root) node is created in the view controller:
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(Node(x: 100, y: 100, width: 150, height: 40))
}
For the root node, everything works as expected. However, for the children of the root node, nothing works.
To make sure that I wasn't using the UITapGestureRecognizer incorrectly, I tried to override the raw touchesBegan
function for the Node class and do a simple println("tapped"). However the problem persisted and seems to be that the subviews do not receive any touches at all.
What could be causing this?