美文网首页
ReactiveCocoa(RAC)初探

ReactiveCocoa(RAC)初探

作者: _相信自己_ | 来源:发表于2019-07-29 01:13 被阅读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]);
    }];

相关文章

网友评论

      本文标题:ReactiveCocoa(RAC)初探

      本文链接:https://www.haomeiwen.com/subject/pqbmrctx.html