I have a label
with isUserInteractionEnabled
set to true
. Now, I need to add UITapGestureRecognizer
for the label. Is there a way to add in Rx
way.
I have looked at the RxSwift library here. Which they didn't provide any extension for adding gesture. The UILabel+Rx
file has only text
and attributedText
.
Is there any workaround to add gesture to label?
A UILabel is not configured with a tap gesture recognizer out of the box, that's why RxCocoa does not provide the means to listen to a gesture directly on the label. You will have to add the gesture recognizer yourself. Then you can use Rx to observe events from the recognizer, like so:
let disposeBag = DisposeBag()
let label = UILabel()
label.text = "Hello World!"
let tapGesture = UITapGestureRecognizer()
label.addGestureRecognizer(tapGesture)
tapGesture.rx.event.bind(onNext: { recognizer in
print("touches: \(recognizer.numberOfTouches)") //or whatever you like
}).disposed(by: disposeBag)