ReactiveCocoa(RAC)初探

2019-07-29  本文已影响0人  _相信自己_

Notification通知

   [[[NSNotificationCenter defaultCenter] 
            rac_addObserverForName:UIKeyboardWillShowNotification object:nil] 
                     subscribeNext:^(NSNotification * _Nullable x) {
        NSLog(@"%@", x);
    }];

ControlEvent事件

[[self.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] 
                   subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];

GestureRecognizer手势作

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
       NSLog(@"%@", x);
    }];
    [self.sendCodeLabel addGestureRecognizer:tap];

KVO

[RACObserve(self.textField, text) subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];

Delegate代理

    @weakify(self)
    // 代理去注册文本框的监听方法
   [[self rac_signalForSelector:@selector(textFieldShouldReturn:) 
                   fromProtocol:@protocol(UITextFieldDelegate)]
                  subscribeNext:^(RACTuple * _Nullable x) {
         @strongify(self)
         if (self.nameText.hasText) {
             [self.passWordText becomeFirstResponder];
         }
     }];
    self.nameText.delegate = self;

遍历 NSArray

    NSArray *numbers = @[@"1",@"2",@"3",@"4"];
    [numbers.rac_sequence.signal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];

遍历 NSDictionary

NSDictionary *dict =@{@"name":@"小马",@"sex":@"男",@"age":@18};
   [dict.rac_sequence.signal subscribeNext:^(id x) {    
       RACTupleUnpack(NSString *key, NSString *value) = x;    
       NSLog(@"key = %@ value = %@",key,value);    
   }];

定时器timer

// 主线程执行
[[RACSignal interval:1.0f onScheduler:[RACScheduler mainThreadScheduler]] 
                        subscribeNext:^(NSDate * _Nullable x) {
        NSLog(@"%@", x);
    }];

// 子线程执行
[[RACSignal interval:1 
         onScheduler:[RACScheduler schedulerWithPriority:RACSchedulerPriorityHigh
                name:@"com.ReactiveCocoa.RACScheduler.mainThreadScheduler"]] 
       subscribeNext:^(NSDate * _Nullable x) {
        NSLog(@"%@", [NSThread currentThread]);
    }];
上一篇下一篇

猜你喜欢

热点阅读