CombineActive is a lightweight reactive extension library based on Apple's Combine framework, designed specifically for UIKit controls. It provides a clean and intuitive API for observing UI events, making your iOS development more modern and functional.
The library aims to simplify the use of the Combine framework with UIKit, offering convenient methods similar to RxSwift/RxCocoa but built on the native Combine framework.
- Reactive UI Event Observing - Transform common UI control events into Combine Publishers
- Memory Safety Management - Built-in DisposeBag mechanism for automatic subscription lifecycle management
- Extensive Control Support - Supports most commonly used UIKit controls
- Clean API Design - Access all extension features through the .cx namespace
- Lightweight Implementation - No external dependencies, focusing only on providing essential extension features
- UIButton - tap events
- UITextField - text, attributedText, editing events
- UITextView - text, attributedText, editing, selection events
- UISlider - value change events
- UISwitch - value change events
- UIBarButtonItem - tap events
- UIView - gesture events (tap, long press, pan)
- Generic UIControl event support
CombineActive is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'CombineActive'
Then run:
pod install
Import the CombineActive framework:
import CombineActive
import Combine
import CombineActive
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
private var disposeBag = CxDisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
button.cx.tap
.sink { _ in
print("Button tapped!")
}
.disposed(by: disposeBag)
}
}
textField.cx.text
.sink { text in
print("Text changed: \(text ?? "")")
}
.disposed(by: textField.cx.disposeBag)
textField.cx.didBegin
.sink {
print("Begin editing")
}
.disposed(by: textField.cx.disposeBag)
button.cx.tap
.sink { _ in
print("This subscription will be automatically cancelled when the ViewController is deallocated")
}
.disposed(by: cx.disposeBag)
// Create a timer that emits every second
Timer.intervalMain(1.0)
.sink { timeInterval in
print("Time interval: \(timeInterval)")
}
.disposed(by: cx.disposeBag)
// Delay on main queue
publisher.delayMain(1.0)
// Debounce on main queue
publisher.debounceMain(0.5)
// Throttle on main queue
publisher.throttleMain(0.5)
To run the example project, clone the repo, and run pod install
from the Example directory first.
- iOS 13.0+
- Swift 5.0+
- Xcode 11.0+
lalawue, [email protected]
CombineActive is available under the MIT license. See the LICENSE file for more info.