Last active
August 1, 2018 08:45
-
-
Save iamchiwon/d315c6a54aaecb6d35c29d4870b8e365 to your computer and use it in GitHub Desktop.
Control 이벤트에 sender 를 알게 한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Rx+Control.swift | |
// ReactiveSample | |
// | |
// Created by iamchiwon on 2018. 8. 1.. | |
// Copyright © 2018년 iamchiwon. All rights reserved. | |
// | |
import UIKit | |
import RxSwift | |
import RxCocoa | |
struct TargetedControlEvent<T> { | |
var event: UIControlEvents | |
var sender: T | |
} | |
extension Reactive where Base: UIControl { | |
func controlEvent(event: UIControlEvents) -> Observable<TargetedControlEvent<Base>> { | |
let targetedControlEvent = TargetedControlEvent(event: event, sender: base) | |
return base.rx.controlEvent(event).map({ _ in targetedControlEvent }) | |
} | |
func controlEvent(events: [UIControlEvents]) -> Observable<TargetedControlEvent<Base>> { | |
return Observable.merge(events.map({ controlEvent(event: $0) })) | |
} | |
var allTouchEvents: Observable<TargetedControlEvent<Base>> { | |
let touchEvents: [UIControlEvents] = [ | |
.touchDownRepeat, | |
.touchDragInside, | |
.touchDragOutside, | |
.touchDragEnter, | |
.touchDragExit, | |
.touchUpInside, | |
.touchUpOutside, | |
.touchCancel | |
] | |
return controlEvent(events: touchEvents) | |
} | |
var allEditingEvents: Observable<TargetedControlEvent<Base>> { | |
let editingEvents: [UIControlEvents] = [ | |
.editingDidBegin, | |
.editingChanged, | |
.editingDidEnd, | |
.editingDidEndOnExit | |
] | |
return controlEvent(events: editingEvents) | |
} | |
var allEvents: Observable<TargetedControlEvent<Base>> { | |
let events: [UIControlEvents] = [ | |
.valueChanged, | |
.applicationReserved, | |
.systemReserved | |
] | |
var eventObservables = events.map({ controlEvent(event: $0) }) | |
eventObservables.append(contentsOf: [allTouchEvents, allEditingEvents]) | |
return Observable.merge(eventObservables) | |
} | |
} |
Author
iamchiwon
commented
Aug 1, 2018
•
button.rx.allEvents
.subscribe(onNext: { e in
print("event : \(e.event)")
print("sender : \(e.sender)")
})
.disposed(by: disposeBag)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment