欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Swift

當(dāng)前位置:主頁 > 軟件編程 > Swift >

RxSwift實現(xiàn)替換delegate的方法示例

來源:本站原創(chuàng)|時間:2020-01-11|欄目:Swift|點擊: 次

目標

最近寫項目 ,寫到需要為自己寫的一個控件添加rx訂閱方式的案例。

目前有一個代理:

// 代理方式獲取結(jié)果
@objc public protocol ZZPhotoPickerControllerDelegate : NSObjectProtocol {
 @objc optional func photoPickerController(_ photoPickerController: ZZPhotoPickerController, didSelect assets: [Any])
}

需要寫一個能夠?qū)崿F(xiàn)下邊這種方式的擴展

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<IndexPath> {
  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產(chǎn)生。delegate是什么delegate?為什么會有methodInvoked方法?于是繼續(xù)點進去。

extension Reactive where Base: UIScrollView {
   /// ...這部分代碼省略不用看

  /// Reactive wrapper for `delegate`.
  ///
  /// For more information take a look at `DelegateProxyType` protocol documentation.
  public var delegate: DelegateProxy<UIScrollView, UIScrollViewDelegate> {
   return RxScrollViewDelegateProxy.proxy(for: base)
  }
  
  /// ...后面的代碼暫時也不用看
}

可以看到delegate是一個DelegateProxy<UIScrollView, UIScrollViewDelegate>類型,根據(jù)字面是理解就是代理的代理。然后還看到這里的rx是擴展自UIScrollView的,UICollectionView是繼承自UIScrollView,可以知道這里的delegate也是繼承過來的使用的。還可以看到RxScrollViewDelegateProxy這個東西,可以想到如果我們要仿寫的話,自己也應(yīng)該寫這樣一個代理的代理類。先點進去看看:

open class RxScrollViewDelegateProxy
 : DelegateProxy<UIScrollView, UIScrollViewDelegate>
 , 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) }
 }

 /// ...后面的感覺沒什么關(guān)系,先不看
}

可以看到它其實是一個DelegateProxy<UIScrollView, UIScrollViewDelegate>,并且遵守了DelegateProxyType和UIScrollViewDelegate協(xié)議,可以感覺出它是一個鏈接rx和delegate的紐帶。有一個實例變量scrollView,有一個init方法,有一個registerKnownImplementations靜態(tài)方法。

現(xiàn)在腦海中大概有一個模糊的思路:我們要先創(chuàng)建一個紐帶delegateProxy對象,然后在目標類的rx擴展中創(chuàng)建一個delegateProxy實例,最后在我們的assetsSelected事件流中用這個delegateProxy的methodInvoked截獲delegate中的目標方法,并生成可訂閱的Observable返回給controlEvent,這樣鏈接打通。

開動

首先創(chuàng)建一個RxPhotoPickerControllerDelegateProxy

class RxPhotoPickerControllerDelegateProxy: DelegateProxy<ZZPhotoPickerController, ZZPhotoPickerControllerDelegate>, 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) }
 }
 
 // 把上面的寫好后,編輯器會提示你需要實現(xiàn)一下兩個方法,一個是獲取,一個是設(shè)置,所以很好理解該在方法里實現(xiàn)什么。
 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<ZZPhotoPickerController, ZZPhotoPickerControllerDelegate> {
  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并沒有開放出來,是個內(nèi)部方法,如果照著寫報錯??梢匝芯砍鲈摲椒ㄖ皇且粋€類型推斷而已,所以可以簡單寫。

完成

然后就可以愉快的去對assetsSelected進行訂閱了。

vc.rx.assetsSelected.subscribe(onNext: { (assets) in
    // do something
   }).disposed(by: self.disposeBag)

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對我們的支持。

上一篇:沒有了

欄    目:Swift

下一篇:沒有了

本文標題:RxSwift實現(xiàn)替換delegate的方法示例

本文地址:http://mengdiqiu.com.cn/a1/Swift/11919.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有