Skip to content

Instantly share code, notes, and snippets.

@nanoxd
Last active September 22, 2018 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanoxd/7d6efed6fc9fc0358a42cbc010b33f40 to your computer and use it in GitHub Desktop.
Save nanoxd/7d6efed6fc9fc0358a42cbc010b33f40 to your computer and use it in GitHub Desktop.
[UIButton+IsCheckedRx] Adds checked behavior to UIButton #uikit #swift
extension Reactive where Base: UIButton {
/// Alternated with button taps to provide
/// a checkbox behavior by `UIButton`
var checked: ControlProperty<Bool> {
let button = self.base
let source: Observable<Bool> = self.tap
.map {[weak button] in
guard let state = button?.isSelected else {
return false
}
return !state
}
let binder: AnyObserver<Bool> = self.isSelected.asObserver()
return ControlProperty(values: source, valueSink: binder)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment