Reputation: 5367
I have created a subclass of UIView
and added in Views by referencing an xib file.
I overrided the touchesBegan
and touchesEnd
functions in order to have customized effect of "color changes when user presses on this view".
Below is this subclass:
class CustomUIView: UIView {
@IBOutlet var contentView: UIView!
override init (frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init? (coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit () {
Bundle.main.loadNibNamed("CustomUIView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch begin")
super.touchesBegan(touches, with: event)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch ended")
super.touchesEnded(touches, with: event)
}
}
However, touchesBegan
and touchesEnd
are never called.
I googled and stackoverflowed, tried for 3 hours already, but cannot find any working solution to this simple question....
Below are what I have tried:
IBAction
of touchUpInside to see whether this IBAction can be triggered. The answer is yes.beginTracking
instead. But it is also not triggered.Below are the properties I can see from ViewHierarchy of this CustomView:
<nil color>
(I have also tried to set it to have a color. Not working.)Help very much appreciated!
Upvotes: 3
Views: 7527
Reputation: 5367
Thanks to @Fahri Azimov 's comment, the reason is contentView
has User Interaction Enabled On. Therefore it consumed all touch events before passing to CustomUIView
. I have upvoted his comment.
The lesson is, the rootView of the xib file is not CustomUIView
itself but one of its child.
Upvotes: 3
Reputation: 3888
Kindly try this, its due to frame of your CustomView
class CustomUIView: UIView {
var contentView: UIView!
override init (frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init? (coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func loadFromNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
private func commonInit () {
contentView = self.loadFromNib()
addSubview(contentView)
contentView.frame = self.bounds
self .isUserInteractionEnabled = true
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch begin")
super.touchesBegan(touches, with: event)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch ended")
super.touchesEnded(touches, with: event)
}
}
I can see a logs in console
Drag one UIView in your ViewController and set your customview class as CustomUIView
Upvotes: 0