美文网首页
01-ReactiveObjC使用记录

01-ReactiveObjC使用记录

作者: CoderLJW | 来源:发表于2018-01-03 20:46 被阅读31次

RAC 的核心思想:创建信号 - 订阅信号 - 发送信号 ,并且在 RAC 中我们会看到大量的 block

RACObserve用法

@interface ViewController ()
/**  */
@property (nonatomic, copy) NSString *username;
/**  */
@property (nonatomic, copy) NSString *password;
/**  */
@property (nonatomic, copy) NSString *content;
@end
------------------------------------------------------

// 监听self这个对象里面的一个username属性
[RACObserve(self, username) subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
}];
// 因为接受过来的是NSString则可以这样写
[RACObserve(self, username) subscribeNext:^(NSString *string) {
        NSLog(@"%@", string);
}];
// 带有过滤效果
    [[RACObserve(self, username) filter:^BOOL(NSString *string) {
        // 根据条件是否发送出值
        return [string containsString:@"aa"];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@", x);
    }];
// RAC() 宏可以使得绑定表示的更清楚  
// combineLatest填入多个监听   reduce根据数组顺序 返回监听到的内容 
// 返回的值会被设置到RAC中的content中
    RAC(self, content) = [RACSignal combineLatest:@[RACObserve(self, username), RACObserve(self, password)] reduce:^(NSString *name, NSString *pass){
        return [NSString stringWithFormat:@"%@%@", name, pass];
    }];
/* 监听 TextField 的输入(内容改变就会调用) */
[[textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
    NSLog(@"输入框内容:%@", x);
}];
/* 添加监听条件 */
[[textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
    return value.length > 5; // 表示输入文字长度 > 5 时才会调用下面的 block
}] subscribeNext:^(NSString * _Nullable x) {
     NSLog(@"输入框内容:%@", x);
}];
// 按钮的监听事件
[[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
    NSLog(@"%@ 按钮被点击了", x); // x 是 button 按钮对象
}];
// RAC接受返回值给loginButton对象的enabled属性赋值
RAC(_loginButton, enabled) = [RACSignal combineLatest:@[_username.rac_textSignal, _password.rac_textSignal] reduce:^id _Nullable(NSString * username, NSString * password){
    return @(username.length && password.length);
}];
// 替代通知  我们可以省去在dealloc中清除通知
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
    NSLog(@"%@ 键盘弹起", x); // x 是通知对象
}];
// 当前类中的btnClick方法执行了,就会有回调
[[self rac_signalForSelector:@selector(btnClick)] subscribeNext:^(RACTuple * _Nullable x) {
    NSLog(@" view 中的按钮被点击了");
}];
// 监听属性改变
[[button rac_valuesForKeyPath:@"frame" observer:self] subscribeNext:^(id  _Nullable x) {
    NSLog(@"属性的改变:%@", x); // x 是监听属性的改变结果
}];
// 监听属性改变 另一种写法
[RACObserve(button, frame) subscribeNext:^(id  _Nullable x) {
    NSLog(@"属性的改变:%@", x); // x 是监听属性的改变结果
}];
  // 定时器功能   3秒后执行一次
  [[RACScheduler mainThreadScheduler]afterDelay:3 schedule:^{
      NSLog(@"3秒后执行一次");
  }];
  // 定时器  每隔两秒执行一次
 //  这里要加takeUntil条件限制一下否则当控制器pop后依旧会执行 
[[[RACSignal interval:2 onScheduler:[RACScheduler mainThreadScheduler]] takeUntil:self.rac_willDeallocSignal ] subscribeNext:^(id x) {
 NSLog(@"每两秒执行一次"); 
}];

相关文章

  • 01-ReactiveObjC使用记录

    RAC 的核心思想:创建信号 - 订阅信号 - 发送信号 ,并且在 RAC 中我们会看到大量的 block RAC...

  • mtrace-内存使用追踪(内存)

    1. 函数格式: mtrace 用于开启内存使用记录,muntrace用于取消内存使用记录。内存使用情况记录到一个...

  • 学习MySQL---ing

    @[toc] Mac MySQL使用教程 本文记录在我的GitHub 记录使用Homebrew安装Mysql全过...

  • 转场动画

    presentation使用记录

  • [tf]tensorboard的使用

    使用tf.summary.scalar记录标量数据,使用tf.summary.histogram直接记录变量var...

  • iOS下FMDB的多线程操作(二)

    上一篇记录不使用FMDatabaseQueue来使用多线程,这一篇记录一下使用FMDatabaseQueue的方式...

  • 后端ing

    spring-cache使用记录 spring-cache的使用记录,坑点记录以及采用的解决方案深入分析 java...

  • vim 插件使用记录

    vim 插件使用 @(linux 编程)[开发技能, 工具使用] 前面记录过一篇vim操作记录此处记录一些vim的...

  • 美柚产品体验报告

    1、你平时有使用软件记录或者管理经期吗? 平时会使用薄荷健康记录经期,因为有段时间使用薄荷健康记录每餐的热量,发现...

  • MariaDB 数据库初始化批处理 Linux

    记录,方便以后使用

网友评论

      本文标题:01-ReactiveObjC使用记录

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