I have done in OC like this:
#import "ViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
@interface ViewController () <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *btn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI];
}
- (void)initUI {
__weak typeof(self) weakSelf = self;
[[RACObserve(self.btn, enabled) map:^id(id value) {
return [value boolValue] ? [UIColor greenColor] : [UIColor redColor];
}] subscribeNext:^(id x) {
[weakSelf.btn setTitleColor:x forState:UIControlStateNormal];
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.btn.enabled = !self.btn.enabled;
}
@end
Now, i try to achieve the same goal in swift use the lastest Version of ReactiveCocoa, how to do?
ReactiveCocoa 5.0 will add UIKit Extensions.
button.reactive.values(forKeyPath:)
This works for me in a simple Playground:
button.reactive.values(forKeyPath: "enabled")
.map { $0 as? Bool }
.skipNil()
.map { enabled in enabled ? UIColor.blue : UIColor.red }
.startWithValues { [weak button = button] color in
button?.setTitleColor(color, for: .normal)
}
However, this is not encouraged because
In this example a simple MutableProperty
is used as model, this could be in a ViewModel or wherever
let buttonEnabled = MutableProperty<Bool>(false)
button.reactive.isEnabled <~ buttonEnabled
buttonEnabled.producer
.map { enabled in enabled ? UIColor.blue : UIColor.red }
.startWithValues { [weak button = button] color in
button?.setTitleColor(color, for: .normal)
}
Unfortunaetly you can not bind directly to the buttons title color as you can with isEnabled