美文网首页
RxSwift 手势

RxSwift 手势

作者: yyggzc521 | 来源:发表于2021-12-08 00:51 被阅读0次

之前用RxSwift的手势是这样的:

  1. 创建一个UITapGestureRecognizer
  2. 给View加入手势识别
  3. 用Rx的方式监听手势事件
let myView: UIView = UIView()

let tapGesture = UITapGestureRecognizer()
myView.addGestureRecognizer(tapGesture)

tapGesture.rx.event
    .debug("Tap")
    .subscribe()
    .disposed(by: disposeBag)

当遇到RxGesture后,一切都变了下面这样

  • 点击
let myView: UIView = UIView()

myView.rx.tapGesture()
    .when(.recognized)
    .subscribe()
    .disposed(by: disposeBag)

使用when过滤手势的状态UIGestureRecognizerState

  • 双击
myView.rx.tapGesture() { gesture, _ in
        gesture.numberOfTapsRequired = 2
    }
    .when(.recognized)
    .subscribe(onNext: { _ in
        print("Double Click")
    })
    .disposed(by: disposeBag)
  • 长按
myView.rx.longPressGesture()
    .when(.began)
    .subscribe(onNext: { _ in
        print("Long Press")
    })
    .disposed(by: disposeBag)
  • 滑动
myView.rx.swipeGesture(.up, .down)
    .when(.recognized)
    .subscribe(onNext: {
        print("Swipe \($0.direction.rawValue)")
    })
    .disposed(by: disposeBag)

资料

相关文章

网友评论

      本文标题:RxSwift 手势

      本文链接:https://www.haomeiwen.com/subject/ebuzxrtx.html