RxSwift package used for Mjrefresh

Source: Internet
Author: User
Tags uicontrol

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 UIControlThe adaptation is done through an intermediate layer ControlTargetTo finish, in order to keep this ControlTargetThe instance survives so that it is not automatically freed, it is wrapped with a set and set as the target UIControlThe associated object. At this point we can put this middle layer ControlTargetAs a resource in this event flow pipeline, the destruction of this resource is determined by the target UIControlto 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.