博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
public var tap: ControlEvent
阅读量:6229 次
发布时间:2019-06-21

本文共 2631 字,大约阅读时间需要 8 分钟。

extension Reactive where Base: UIButton {

    

    /// Reactive wrapper for `TouchUpInside` control event.

    public var tap: ControlEvent<Void> {

        return controlEvent(.touchUpInside)

    }

}

 

extension Reactive where Base: UIControl {

 

    public func controlEvent(_ controlEvents: UIControlEvents) -> ControlEvent<()> {

        let source: Observable<Void> = Observable.create { [weak control = self.base] observer in

                MainScheduler.ensureExecutingOnScheduler()

 

                guard let control = control else {

                    observer.on(.completed)

                    return Disposables.create()

                }

 

                let controlTarget = ControlTarget(control: control, controlEvents: controlEvents) {

                    control in

                    observer.on(.next(()))

                }

 

                return Disposables.create(with: controlTarget.dispose)

            }

            .takeUntil(deallocated)

 

        return ControlEvent(events: source)

    }

}

 

final class ControlTarget: RxTarget {

    typealias Callback = (Control) -> Void

 

    let selector: Selector = #selector(ControlTarget.eventHandler(_:))

 

    weak var control: Control?

#if os(iOS) || os(tvOS)

    let controlEvents: UIControlEvents

#endif

    var callback: Callback?

    #if os(iOS) || os(tvOS)

    init(control: Control, controlEvents: UIControlEvents, callback: @escaping Callback) {

        MainScheduler.ensureExecutingOnScheduler()

 

        self.control = control

        self.controlEvents = controlEvents

        self.callback = callback

 

        super.init()

 

        control.addTarget(self, action: selector, for: controlEvents)

 

        let method = self.method(for: selector)

        if method == nil {

            rxFatalError("Can't find method")

        }

    }

#elseif os(macOS)

    init(control: Control, callback: @escaping Callback) {

        MainScheduler.ensureExecutingOnScheduler()

 

        self.control = control

        self.callback = callback

 

        super.init()

 

        control.target = self

        control.action = selector

 

        let method = self.method(for: selector)

        if method == nil {

            rxFatalError("Can't find method")

        }

    }

#endif

 

    @objc func eventHandler(_ sender: Control!) {

        if let callback = self.callback, let control = self.control {

            callback(control)

        }

    }

 

    override func dispose() {

        super.dispose()

#if os(iOS) || os(tvOS)

        self.control?.removeTarget(self, action: self.selector, for: self.controlEvents)

#elseif os(macOS)

        self.control?.target = nil

        self.control?.action = nil

#endif

        self.callback = nil

    }

}

 

public struct ControlEvent<PropertyType> : ControlEventType {

    public typealias E = PropertyType

 

    let _events: Observable<PropertyType>

 

    /// Initializes control event with a observable sequence that represents events.

    ///

    /// - parameter events: Observable sequence that represents events.

    /// - returns: Control event created with a observable sequence of events.

    public init<Ev: ObservableType>(events: Ev) where Ev.E == E {

        _events = events.subscribeOn(ConcurrentMainScheduler.instance)

    }

}

转载地址:http://bfxna.baihongyu.com/

你可能感兴趣的文章
非面试向跨域实践详解
查看>>
一个非常好看的图片选择框架LPhotoPicker,确定不来看看么
查看>>
线上压缩代码-定位错误
查看>>
一个简洁且强大的状态管理库 - iFlow
查看>>
IP地址转换函数——inet_pton inet_ntop inet_aton inet_addr inet_ntoa
查看>>
设计模式笔记---4. 装饰模式
查看>>
springmvc + mybatis + ehcache + redis 分布式架构
查看>>
爬虫学习日记(四)分析Freenium
查看>>
nginx事件模块 -- 第五篇 epoll add
查看>>
共享栈基本操作
查看>>
Java 生成 PDF 文档
查看>>
深度学习:用生成对抗网络(GAN)来恢复高分辨率(高精度)图片 (附源码,模型与数据集)...
查看>>
缓存与数据库双写,不一致问题及解决方案
查看>>
Swift基础-部分关键字说明与示例
查看>>
【云服务月刊】2018年第1期:阿里云客户服务部总经理张颖杰:用心聆听,服务见智...
查看>>
99%的Java程序员都不知道的Spring中的@Transactional注解的坑
查看>>
堆排序 Heap Sort
查看>>
golang map 底层部分理解
查看>>
3.22(终)
查看>>
第61节:Java中的DOM和Javascript技术
查看>>