gpt4 book ai didi

ios - Rx swift : How to add gesture to UILabel?

转载 作者:IT王子 更新时间:2023-10-29 05:24:39 24 4
gpt4 key购买 nike

我有一个 label,其中 isUserInteractionEnabled 设置为 true。现在,我需要为标签添加 UITapGestureRecognizer。有没有办法以 Rx 方式添加。

我查看了 RxSwift 库 here .他们没有为添加手势提供任何扩展。 UILabel+Rx 文件只有 textattributedText

是否有任何解决方法可以将手势添加到标签?

最佳答案

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 - Rx swift : How to add gesture to UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44630176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com