Tl;DR: A UIView
sublass attracts some customized shapes into its body. The way to solely reply to touches inside these shapes?
Particulars:
Assume we wish to create a customized bar chart. BarChartView
receives some information array as enter and for every merchandise it advertisements a BarView
as subview.
The body of every BarView
is just like the BarChartView
s bounds. Nevertheless, many of the BarView
is clear, it handles drawing its bar (a easy rect) to the proper location.
Now I wish to detect touches throughout the bars. Not throughout the full BarView
(which covers the entire BarChartView
) however solely throughout the drawn bars/rects.
How can I do that?
isUserInteractionEnabled
is true for the BarChartView
and all its BarView
s. Nevertheless solely the highest most BarView
does obtain the contact occasion. How can this view inform iOS “this contact shouldn’t be dealt with by me, cross it to the opposite views”?
I attempted to override hitTest
however this doesn’t work:
class BarView: UIView {
...
override func hitTest(_ level: CGPoint, with occasion: UIEvent?) -> UIView? {
print("hitTest: (information.index)")
if touchInsideBarRect(level) {
print(" inside")
return self // Return self to point that the contact is handeled
}
return superview // Return father or mother BarChartView to let others BarViews deal with the contact
}
}
Output reveals that the highest most BarView
accurately detects wether the contact was contained in the bar or not. Nevertheless, if the contact was outdoors and superview
is returned, the opposite BarView
s usually are not referred to as to test in the event that they deal with the contact.