zoukankan      html  css  js  c++  java
  • public var tap: ControlEvent

    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)

        }

    }

  • 相关阅读:
    DB2中的ROW_NUMBER() OVER()函数用法 转
    各个数据库分页语句汇总
    db2导出DDL
    db2数据库操作
    在myeclipse+tomcat中出现org.apache.juli.logging.LogFactory这样的错误,为什么?
    spring的事务管理有几种方式实现 (转自:http://blog.csdn.net/bopzhou/article/details/7094108)
    JAVA 判断一个字符串是不是一个合法的日期格式
    oracle添加字段 与 db2 添加字段
    ajax详细讲解
    Js 时间间隔计算(间隔天数)
  • 原文地址:https://www.cnblogs.com/feng9exe/p/9016606.html
Copyright © 2011-2022 走看看