iOS-ReactiveObjC 的使用

 // KVO 监听属性
    [[self rac_valuesAndChangesForKeyPath:@"age" options:NSKeyValueObservingOptionNew observer:self] subscribeNext:^(RACTwoTuple * _Nullable x) {
        NSLog(@"%@ - %@", x.first, x.second);
    }];

 // 监听button点击事件
    [[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        self.age++;
    }];

 // 监听textField的变化
    [[self.textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"%@", x);
    }];

 // textField 文字发生变化时, label也变
    RAC(self.label, text) = self.textField.rac_textSignal;

    // RAC 手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        NSLog(@"");
    }];
    [self.view addGestureRecognizer:tap];

    // RAC 过滤器
    [[self.textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        return value.length > 3; // 满足这个条件就发送信号, 执行订阅的内容
    }] subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"%@", x);
    }] ;

    // RAC bind 绑定/包装
    [[self.textField.rac_textSignal bind:^RACSignalBindBlock _Nonnull{

        return ^RACSignal *(id value, BOOL *stop) {
            // 做好处理,通过信号返回出去.
            // 需要引入头文件 #import 
            return [RACSignal return:[NSString stringWithFormat:@"hello : %@", value]];
        };
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x); // hello : x
    }];


    // RAC flattenMap 映射
    [[self.textField.rac_textSignal flattenMap:^__kindof RACSignal * _Nullable(NSString * _Nullable value) {

        return [RACSignal return:[NSString stringWithFormat:@"hello : %@", value]];

    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x); // hello : x
    }];

    // RAC Map 映射
    [[self.textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
        return [NSString stringWithFormat:@"%@", value];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x); // hello : x
    }];

//     RAC concat 合并: 按一定顺序拼接信号,当多个信号发出的时候,有顺序的接收信号
//                     有多少个信号, 订阅就执行多少次
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"信号A"];
        [subscriber sendCompleted];
        return nil;
    }];
    RACSignal *signalB = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"信号B"];
        [subscriber sendCompleted];
        return nil;
    }];
    // signal 必须要有信号发送
    [[signalA concat:signalB] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];

    // RAC then : 当第一个信号完成,才会连接then返回的信号, 只会接收到后面的信号数据
    [[[RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        [subscriber sendCompleted];
        return nil;
    }] then:^RACSignal * _Nonnull{
        return [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
            [subscriber sendNext:@"2"];
            [subscriber sendCompleted];
            return nil;
        }];
    }] subscribeNext:^(id  _Nullable x) { // 只接受到后面的信号数据
        NSLog(@"%@", x); // 2
    }];

    // RAC merge 合并 相当于 ||, 任何信号有新值都会调用, 与concat的不同, concat 的signalA 必须要发送信号
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        [subscriber sendCompleted];
        return nil;
    }];
    RACSignal *signalB = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"2"];
        [subscriber sendCompleted];
        return nil;
    }];
    [[signalA merge:signalB] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];

    // zipWith 压缩, 必须同时发出信号内容
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        [subscriber sendCompleted];
        return nil;
    }];
    RACSignal *signalB = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"2"];
        [subscriber sendCompleted];
        return nil;
    }];
    [[signalA zipWith:signalB] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];

//    RAC combineLatest 结合, 与zipWith相同, 必须2个信号同时发送
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        [subscriber sendCompleted];
        return nil;
    }];
    RACSignal *signalB = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"2"];
        [subscriber sendCompleted];
        return nil;
    }];
    [[signalA combineLatestWith:signalB] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];

    // RAC reduce 聚合 2个信号必须都发送, 先把信号聚集再发送  订阅
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        [subscriber sendCompleted];
        return nil;
    }];
    RACSignal *signalB = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"2"];
        [subscriber sendCompleted];
        return nil;
    }];
    RACSignal *reduceSignal = [RACSignal combineLatest:@[signalA, signalB] reduce:^id (NSString *num1, NSString *num2){
        return [NSString stringWithFormat:@"%@, %@", num1, num2];
    }];
    [reduceSignal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];

    // RAC ignore 忽略
    // 当self.textField.text = 6 时不发送信号
    [[self.textField.rac_textSignal ignore:@"6"] subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"%@", x);
    }];

    // RAC 定时器
    [[RACSignal interval:1 onScheduler:[RACScheduler currentScheduler]] subscribeNext:^(NSDate * _Nullable x) {
        NSLog(@"%@", x);
    }];

    // RAC delay 延迟
    [[[RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        [subscriber sendCompleted];
        return nil;
    }] delay:5] subscribeNext:^(id  _Nullable x) { // 5秒后执行
        NSLog(@"%@", x);
    }] ;

你可能感兴趣的:(iOS-ReactiveObjC 的使用)