目标
最近写项目 ,写到需要为自己写的一个控件添加rx订阅方式的案例。
目前有一个代理:
// 代理方式获取结果
@objc public protocol ZZPhotoPickerControllerDelegate : NSObjectProtocol {
@objc optional func photoPickerController(_ photoPickerController: ZZPhotoPickerController, didSelect assets: [Any])
}
需要写一个能够实现下边这种方式的扩展
photoPickerController.rx.assetsSelected.subscribe(onNext: { assets in
// do something
}
思路
刚开始完全摸不着头脑。后来想到Rx写了对UICollectionViewDelegate的扩展:
collectionView.rx.itemSelected.subscribe(onNext: { indexPath in
// do something
}
跟我的需求是一样的。
于是就去看itemSelected的源代码:
/// Reactive wrapper for `delegate` message `collectionView(_:didSelectItemAtIndexPath:)`.
public var itemSelected: ControlEvent {
let source = delegate.methodInvoked(#selector(UICollectionViewDelegate.collectionView(_:didSelectItemAt:)))
.map { a in
return try castOrThrow(IndexPath.self, a[1])
}
return ControlEvent(events: source)
}
souce是一个Observable,由delegate.methodInvoked产生。delegate是什么delegate?为什么会有methodInvoked方法?于是继续点进去。
extension Reactive where Base: UIScrollView {
/// ...这部分代码省略不用看
/// Reactive wrapper for `delegate`.
///
/// For more information take a look at `DelegateProxyType` protocol documentation.
public var delegate: DelegateProxy {
return RxScrollViewDelegateProxy.proxy(for: base)
}
/// ...后面的代码暂时也不用看
}
可以看到delegate是一个DelegateProxy
open class RxScrollViewDelegateProxy
: DelegateProxy
, DelegateProxyType
, UIScrollViewDelegate {
/// Typed parent object.
public weak private(set) var scrollView: UIScrollView?
/// - parameter scrollView: Parent object for delegate proxy.
public init(scrollView: ParentObject) {
self.scrollView = scrollView
super.init(parentObject: scrollView, delegateProxy: RxScrollViewDelegateProxy.self)
}
// Register known implementations
public static func registerKnownImplementations() {
self.register { RxScrollViewDelegateProxy(scrollView: $0) }
self.register { RxTableViewDelegateProxy(tableView: $0) }
self.register { RxCollectionViewDelegateProxy(collectionView: $0) }
self.register { RxTextViewDelegateProxy(textView: $0) }
}
/// ...后面的感觉没什么关系,先不看
}
可以看到它其实是一个DelegateProxy
现在脑海中大概有一个模糊的思路:我们要先创建一个纽带delegateProxy对象,然后在目标类的rx扩展中创建一个delegateProxy实例,最后在我们的assetsSelected事件流中用这个delegateProxy的methodInvoked截获delegate中的目标方法,并生成可订阅的Observable返回给controlEvent,这样链接打通。
开动
首先创建一个RxPhotoPickerControllerDelegateProxy
class RxPhotoPickerControllerDelegateProxy: DelegateProxy, DelegateProxyType, ZZPhotoPickerControllerDelegate {
/// Typed parent object.
public weak private(set) var photoPickerController: ZZPhotoPickerController?
/// - parameter scrollView: Parent object for delegate proxy.
public init(photoPickerController: ParentObject) {
self.photoPickerController = photoPickerController
super.init(parentObject: photoPickerController, delegateProxy: RxPhotoPickerControllerDelegateProxy.self)
}
static func registerKnownImplementations() {
self.register { RxPhotoPickerControllerDelegateProxy(photoPickerController: $0) }
}
// 把上面的写好后,编辑器会提示你需要实现一下两个方法,一个是获取,一个是设置,所以很好理解该在方法里实现什么。
static func currentDelegate(for object: ZZPhotoPickerController) -> ZZPhotoPickerControllerDelegate? {
return object.zzDelegate
}
static func setCurrentDelegate(_ delegate: ZZPhotoPickerControllerDelegate?, to object: ZZPhotoPickerController) {
object.zzDelegate = delegate
}
}
然后给目标的rx扩展写一个delegateProxy实例:
extension Reactive where Base: ZZPhotoPickerController {
public var zzDelegate: DelegateProxy {
return RxPhotoPickerControllerDelegateProxy.proxy(for: base)
}
}
最后写我们的assetsSelected:
extension Reactive where Base: ZZPhotoPickerController {
var assetsSelected: ControlEvent<[Any]> {
let source: Observable<[Any]> = self.zzDelegate.methodInvoked(#selector(ZZPhotoPickerControllerDelegate.photoPickerController(_:didSelect:))).map { a in
return a[1] as! [Any]
}
return ControlEvent.init(events: source)
}
}
要注意里面有个方法castOrThrow,这个方法rx并没有开放出来,是个内部方法,如果照着写报错。可以研究出该方法只是一个类型推断而已,所以可以简单写。
完成
然后就可以愉快的去对assetsSelected进行订阅了。
vc.rx.assetsSelected.subscribe(onNext: { (assets) in
// do something
}).disposed(by: self.disposeBag)