For a very common two libraries, how Mjrefresh can be used like UIButton:
Btn.rx.tap.subscribe (...)
Many of the similar approaches in Rxswift use code that is very similar to the following
To target
UIControl
The adaptation is done through an intermediate layer
ControlTarget
To finish, in order to keep this
ControlTarget
The instance survives so that it is not automatically freed, it is wrapped with a set and set as the target
UIControl
The associated object. At this point we can put this middle layer
ControlTarget
As a resource in this event flow pipeline, the destruction of this resource is determined by the target
UIControl
to decide.
The personal code is as follows:
import uikitimport mjrefreshimport rxswiftimport RxcocoaclassRxtarget:nsobject, Disposable {//Rxtarget is Rxswift source code Privatevar retainself:rxtarget?Overrideinit () {super.init () self.retainself=Self } func Dispose () {self.retainself=Nil}} FinalclassRefreshtarget<component:mjrefreshcomponent>: rxtarget {typealias Callback=mjrefreshcomponentrefreshingblock var callback:callback?weak var component:component?Let selector=#selector (Refreshtarget.eventhandler) init (_ Component:component,callback: @escaping callback) {SELF.CALLB Ack=Callback Self.component=component Super.init () Component.setrefreshingtarget (self, Refreshingaction:selector)} @objc Fu NC eventHandler () {ifLet callback =Self.callback {callback ()}}Overridefunc Dispose () {super.dispose () self.component?. Refreshingblock =Nil Self.callback=Nil}} Extension ReactivewhereBase:mjrefreshcomponent {varEvent: controlevent<base>{Let source:observable<Base> = observable.create {[Weak control = self.Base] ObserverinchMainscheduler.ensureexecutingonscheduler () guard Let control= ControlElse{Observer.on (. completed)returndisposables.create ()} Let observer=Refreshtarget (Control) {Observer.on (. Next (Control)})returnObserver}.takeuntil (deallocated)returncontrolevent (Events:source)}}
How to use it?
CollectionView.mj_header.rx. Event In Reactor.Action.refresh }. Bind (To:reactor.action). Disposed (by:disposebag) CollectionView.mj_footer.rx. Event In Reactor.Action.loadMore }. Bind (To:reactor.action). Disposed (by: Disposebag)
For more information, please go to my gitpage.
RxSwift package used for Mjrefresh