RxGesture
用法
要运行示例项目,请克隆仓库,在Example
文件夹中打开RxGesture.xcworkspace
。
您可能需要先从Example目录运行pod install
命令。
RxGesture允许您轻松地将任何视图转换为可触摸或可滑动控件,如下所示
view.rx
.tapGesture()
.when(.recognized)
.subscribe(onNext: { _ in
//react to taps
})
.disposed(by: stepBag)
您还可以响应多个手势。例如,为了在用户按下、向上或向下滑动时关闭照片预览,您可能希望这样做
view.rx
.anyGesture(.tap(), .swipe([.up, .down]))
.when(.recognized)
.subscribe(onNext: { _ in
//dismiss presented photo
})
.disposed(by: stepBag)
rx.gesture
被定义为Observable<G>
,其中G
是手势识别器的实际类型,因此它发出的实际上是手势识别器本身(如果您想调用像asLocation(in view:)
或asTranslation(in view:)
这样的方法,这很有用)
在iOS上,RxGesture支持
view.rx.tapGesture() -> ControlEvent<UITapGestureRecognizer>
view.rx.pinchGesture() -> ControlEvent<UIPinchGestureRecognizer>
view.rx.swipeGesture(.left) -> ControlEvent<UISwipeGestureRecognizer>
view.rx.panGesture() -> ControlEvent<UIPanGestureRecognizer>
view.rx.longPressGesture() -> ControlEvent<UILongPressGestureRecognizer>
view.rx.rotationGesture() -> ControlEvent<UIRotationGestureRecognizer>
view.rx.screenEdgePanGesture() -> ControlEvent<UIScreenEdgePanGestureRecognizer>
view.rx.hoverGesture() -> ControlEvent<UIHoverGestureRecognizer>
view.rx.anyGesture(.tap(), ...) -> ControlEvent<UIGestureRecognizer>
view.rx.anyGesture(.pinch(), ...) -> ControlEvent<UIGestureRecognizer>
view.rx.anyGesture(.swipe(.left), ...) -> ControlEvent<UIGestureRecognizer>
view.rx.anyGesture(.pan(), ...) -> ControlEvent<UIGestureRecognizer>
view.rx.anyGesture(.longPress(), ...) -> ControlEvent<UIGestureRecognizer>
view.rx.anyGesture(.rotation(), ...) -> ControlEvent<UIGestureRecognizer>
view.rx.anyGesture(.screenEdgePan(), ...) -> ControlEvent<UIGestureRecognizer>
view.rx.anyGesture(.hover(), ...) -> ControlEvent<UIGestureRecognizer>
在macOS上,RxGesture支持
view.rx.clickGesture() -> ControlEvent<NSClickGestureRecognizer>
view.rx.rightClickGesture() -> ControlEvent<NSClickGestureRecognizer>
view.rx.panGesture() -> ControlEvent<NSPanGestureRecognizer>
view.rx.pressGesture() -> ControlEvent<NSPressGestureRecognizer>
view.rx.rotationGesture() -> ControlEvent<NSRotationGestureRecognizer>
view.rx.magnificationGesture() -> ControlEvent<NSMagnificationGestureRecognizer>
view.rx.anyGesture(.click(), ...) -> ControlEvent<NSGestureRecognizer>
view.rx.anyGesture(.rightClick(), ...) -> ControlEvent<NSGestureRecognizer>
view.rx.anyGesture(.pan(), ...) -> ControlEvent<NSGestureRecognizer>
view.rx.anyGesture(.press(), ...) -> ControlEvent<NSGestureRecognizer>
view.rx.anyGesture(.rotation(), ...) -> ControlEvent<NSGestureRecognizer>
view.rx.anyGesture(.magnification(), ...) -> ControlEvent<NSGestureRecognizer>
view.rx.fooGesture()
语法而不是view.rx.anyGesture(.foo())
,因为它返回具体的UIGestureRecognizer
子类,并避免您在subscribe()
中进行转换。
状态过滤
默认情况下,对于手势识别器的状态没有过滤。这意味着您将始终收到带有手势识别器初始状态(几乎总是.possible
)的第一个事件。
以下是每种类型手势(iOS和macOS)可用的首选状态
类型 | 状态 |
---|---|
.tap() .click() .rightClick() .swipe() |
.recognized |
.longPress() .press() |
.began |
.pan() .pinch() .rotation() .magnification() .screenEdgePan() |
.began .changed .ended |
您通常会使用.when()
运算符来过滤状态
view.rx.tapGesture().when(.recognized)
view.rx.panGesture().when(.began, .changed, .ended)
如果您同时观察多个手势,您可以使用.when()
运算符来针对所有手势识别器进行相同的过滤,或者使用元组语法进行单个过滤
view.rx
.anyGesture(.tap(), .swipe([.up, .down]))
.when(.recognized)
.subscribe(onNext: { gesture in
// Called whenever a tap, a swipe-up or a swipe-down is recognized (state == .recognized)
})
.disposed(by: bag)
view.rx
.anyGesture(
(.tap(), when: .recognized),
(.pan(), when: .ended)
)
.subscribe(onNext: { gesture in
// Called whenever:
// - a tap is recognized (state == .recognized)
// - or a pan is ended (state == .ended)
})
.disposed(by: bag)
委托定制
轻盈定制
每个手势识别器都有一个默认的RxGestureRecognizerDelegate
。它允许您使用策略来定制每个代理方法
.always
将始终向相应的代理方法返回true
.never
将始终向相应的代理方法返回false
.custom
接收一个关联的闭包,该闭包将被执行以返回相应的委托方法的价值
以下是可用的策略及其对应的委托方法
beginPolicy -> gestureRecognizerShouldBegin(:_)
touchReceptionPolicy -> gestureRecognizer(_:shouldReceive:)
selfFailureRequirementPolicy -> gestureRecognizer(_:shouldBeRequiredToFailBy:)
otherFailureRequirementPolicy -> gestureRecognizer(_:shouldRequireFailureOf:)
simultaneousRecognitionPolicy -> gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
eventRecognitionAttemptPolicy -> gestureRecognizer(_:shouldAttemptToRecognizeWith:) // macOS only
pressReceptionPolicy -> gestureRecognizer(_:shouldReceive:) // iOS only
此委托可以在配置闭包中进行自定义
view.rx.tapGesture(configuration: { gestureRecognizer, delegate in
delegate.simultaneousRecognitionPolicy = .always // (default value)
// or
delegate.simultaneousRecognitionPolicy = .never
// or
delegate.simultaneousRecognitionPolicy = .custom { gestureRecognizer, otherGestureRecognizer in
return otherGestureRecognizer is UIPanGestureRecognizer
}
delegate.otherFailureRequirementPolicy = .custom { gestureRecognizer, otherGestureRecognizer in
return otherGestureRecognizer is UILongPressGestureRecognizer
}
})
默认值可以在 RxGestureRecognizerDelegate.swift
中找到。
完全自定义
您还可以用自己的委托替换默认的委托,或者删除它。
view.rx.tapGesture { [unowned self] gestureRecognizer, delegate in
gestureRecognizer.delegate = nil
// or
gestureRecognizer.delegate = self
}
要求
此库依赖于 RxSwift 和 RxCocoa。
安装
CocoaPods
将此行添加到 Podfile
pod "RxGesture"
$ pod install
Carthage
将此行添加到 Cartfile
github "RxSwiftCommunity/RxGesture" ~> 3.0
$ carthage update
感谢
在 RxSwift Slack 频道中的每个人
许可协议
RxGesture 在 MIT 许可协议下提供。有关更多信息,请参阅 LICENSE 文件。