DelegateProxy enable you to receive delegate events by subscribed handler.
This is generic version of DelegateProxy by RxSwift
It means be able to use in combination with any other reactive-framework like ReactiveCocoa or SwiftBond, etc.
- Swift 3 / Xcode 8
- OS X 10.9 or later
- iOS 8.0 or later
- watchOS 2.0 or later
- tvOS 9.0 or later
Add the following to your Podfile:
use_frameworks!
target 'YOUR_TARGET_NAME' do
pod 'DelegateProxy'
end
$ pod install
Add the following to your Cartfile:
github "ra1028/DelegateProxy"
$ carthage update
Create DelegateProxy
inherited class.
final class ScrollViewDelegateProxy: DelegateProxy, UIScrollViewDelegate, DelegateProxyType {
func resetDelegateProxy(owner: UIScrollView) {
owner.delegate = self
}
}
It can be useful to implements extension.
extension UIScrollView {
var delegateProxy: ScrollViewDelegateProxy {
return .proxy(for: self)
}
}
You can receive delegate events as following.
scrollView.delegateProxy
.receive(#selector(UIScrollViewDelegate.scrollViewDidScroll(_:))) { args in
guard let scrollView: UIScrollView = args.value(at: 0) else { return }
print(scrollView.contentOffset)
}
You can receive delegate events by Receivable
protocol implemented class.
Followings are examples of use DelegateProxy with reactive-frameworks.
With ReactiveCocoa
Create receiver class.
final class RACReceiver: Receivable {
let (signal, observer) = Signal<Arguments, NoError>.pipe()
func send(arguments: Arguments) {
observer.send(value: arguments)
}
}
Extension
extension DelegateProxy {
func rac_receive(selector: Selector) -> Signal<Arguments, NoError> {
return RACReceiver().subscribe(to: self, selector: selector).signal
}
}
Receive events by streams.
scrollView.delegateProxy
.rac_receive(selector: #selector(UIScrollViewDelegate.scrollViewDidScroll(_:)))
.map { $0.value(at: 0, as: UIScrollView.self)?.contentOffset }
.skipNil()
.observeValues { print("ContentOffset: \($0)") }
With SwiftBond
Create receiver class.
final class BondReceiver: Receivable {
let subject = PublishSubject<Arguments, NoError>()
func send(arguments: Arguments) {
subject.next(arguments)
}
}
Extension
extension DelegateProxy {
func bnd_receive(selector: Selector) -> Signal<Arguments, NoError> {
return BondReceiver().subscribe(to: self, selector: selector).subject.toSignal()
}
}
Receive events by streams.
scrollView.delegateProxy
.bnd_receive(selector: #selector(UIScrollViewDelegate.scrollViewDidScroll(_:)))
.map { $0.value(at: 0, as: UIScrollView.self)?.contentOffset }
.ignoreNil()
.observeNext { print("ContentOffset: \($0)") }
Welcome to fork and submit pull requests!!
Before submitting pull request, please ensure you have passed the included tests.
If your pull request including new function, please write test cases for it.
DelegateProxy is released under the MIT License.