我有一个label
,其isUserInteractionEnabled
设置为true
。现在,我需要为标签添加UITapGestureRecognizer
。有没有一种方法可以添加Rx
方式。
我看过RxSwift库here。他们没有为添加手势提供任何扩展。 UILabel+Rx
文件只有text
和attributedText
。
是否有任何解决方法可将手势添加到标签?
最佳答案
没有为UILabel配置开箱即用的手势识别器,这就是RxCocoa无法提供直接在标签上收听手势的方法的原因。您将必须自己添加手势识别器。然后,您可以使用Rx观察来自识别器的事件,如下所示:
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)
关于ios - RxSwift : How to add gesture to UILabel?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44630176/