RAC学习

RAC导入

通常都会使用CocoaPods帮助我们导入,和其他第三方库不同的时,导入该库需要在Podfile中添加use_frameworks!,如果没有加入会导入出错,并提示你添加use_frameworks!,Podfile内容:
use_frameworks!
pod 'ReactiveCocoa', '~> 4.0.2-alpha-1'

简单的使用

首先导入头文件

#import 

具体使用

#import "ViewController.h"
#import 
#import 

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
    [self performSelector:@selector(postNotice) withObject:nil afterDelay:3];
}

-(void)postNotice
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"testNotice" object:nil];
}

-(void)setupUI
{
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 30)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:textField];
    //处理输入改变
    [[textField rac_signalForControlEvents:UIControlEventEditingChanged]subscribeNext:^(id x) {
        NSLog(@"textField value changed:%@",NSStringFromClass(object_getClass(x)));
    }];
    //处理输入改变
    [[textField rac_textSignal] subscribeNext:^(id x) {
         NSLog(@"textField value changed:%@,type:%@",x,NSStringFromClass(object_getClass(x)));
    }];
    
    //添加手势
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 80, 50)];
    textLabel.backgroundColor = [UIColor redColor];
    textLabel.userInteractionEnabled = YES;
    [self.view addSubview:textLabel];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]init];
    [[tapGesture rac_gestureSignal]subscribeNext:^(id x) {
        
        NSLog(@"tapGesture Action %@,type:%@",x,NSStringFromClass(object_getClass(x)));
    }];
    [textLabel addGestureRecognizer:tapGesture];
    
    //通知
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"testNotice" object:nil] subscribeNext:^(id x) {
       
        NSLog(@"notification %@,type:%@",x,NSStringFromClass(object_getClass(x)));
    }];
}

 //kvo
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 250, 200, 200)];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.backgroundColor = [UIColor redColor];
    [self.view addSubview:tableView];
    
    [RACObserve(tableView, contentOffset) subscribeNext:^(id x) {
       NSLog(@"x %@,type:%@",x,NSStringFromClass(object_getClass(x)));
    }];

@end

你可能感兴趣的:(RAC学习)