RxPagingKit
PagingKit 的响应式扩展 PagingKit
这是什么?
要求
- iOS 8.0+
- Xcode 10.0+
- Swift 4.2
安装
CocoaPods
- 安装 CocoaPods
> gem install cocoapods
> pod setup
- 创建 Podfile
> pod init
- 编辑 Podfile
target 'YourProject' do
use_frameworks!
pod "RxPagingKit" # add
target 'YourProject' do
inherit! :search_paths
end
target 'YourProject' do
inherit! :search_paths
end
end
- 安装
> pod install
打开 .xcworkspace
Carthage
- 使用Homebrew安装Carthage
> ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
> brew update
> brew install carthage
- 移动你的项目目录并创建Cartfile
> touch Cartfile
- 在Cartfile中添加以下行
github "kazuhiro4949/RxPagingKit"
- 创建框架
> carthage update --platform iOS
- 在Xcode中,导航到"通用 > 编译阶段 > 链接的框架和库)
- 将RxPagingKit以及其依赖(PagingKit,RxSwift和RxCocoa)添加到你的项目中
- 添加一个新的运行脚本并放置以下代码
/usr/local/bin/carthage copy-frameworks
- 在“输入文件”处点击“+”并添加框架路径
$(SRCROOT)/Carthage/Build/iOS/RxPagingKit.framework
$(SRCROOT)/Carthage/Build/iOS/PagingKit.framework
$(SRCROOT)/Carthage/Build/iOS/RxSwift.framework
$(SRCROOT)/Carthage/Build/iOS/RxCocoa.framework
- 在你的源文件中编写导入语句
import RxPagingKit
import RxSwift
import RxCocoa
import PagingKit
示例
您可以使用响应式编程来实现绑定,而不是使用代理模式。
let items = PublishSubject<[(menu: String, width: CGFloat, content: UIViewController)]>()
override func viewDidLoad() {
super.viewDidLoad()
menuViewController?.register(type: TitleLabelMenuViewCell.self, forCellWithReuseIdentifier: "identifier")
menuViewController?.registerFocusView(view: UnderlineFocusView())
// PagingMenuViewControllerDataSource
items.asObserver()
.map { items in items.map({ ($0.menu, $0.width) }) }
.bind(to: menuViewController.rx.items(
cellIdentifier: "identifier",
cellType: TitleLabelMenuViewCell.self)
) { _, model, cell in
cell.titleLabel.text = model
}
.disposed(by: disposeBag)
// PagingContentViewControllerDataSource
items.asObserver()
.map { items in items.map({ $0.content }) }
.bind(to: contentViewController.rx.viewControllers())
.disposed(by: disposeBag)
// PagingMenuViewControllerDelegate
menuViewController.rx.itemSelected.asObservable().subscribe(onNext: { [weak self] (page, prev) in
self?.contentViewController.scroll(to: page, animated: true)
}).disposed(by: disposeBag)
// PagingContentViewControllerDelegate
contentViewController.rx.didManualScroll.asObservable().subscribe(onNext: { [weak self] (index, percent) in
self?.menuViewController.scroll(index: index, percent: percent, animated: false)
}).disposed(by: disposeBag)
}