RAC

RAC学习1

2016-04-14  本文已影响253人  7dfa9c18c1d1

什么是RAC

特点
解决问题
学习资料链接

http://spin.atomicobject.com/2014/02/03/objective-c-delegate-pattern/

http://blog.bignerdranch.com/4549-data-driven-ios-development-reactivecocoa/

http://en.wikipedia.org/wiki/Functional_reactive_programming

http://www.teehanlax.com/blog/reactivecocoa/

http://www.teehanlax.com/blog/getting-started-with-reactivecocoa/

http://nshipster.com/reactivecocoa/

http://cocoasamurai.blogspot.com/2013/03/basic-mvvm-with-reactivecocoa.html

http://iiiyu.com/2013/09/11/learning-ios-notes-twenty-eight/

https://speakerdeck.com/andrewsardone/reactivecocoa-at-mobidevday-2013

http://msdn.microsoft.com/en-us/library/hh848246.aspx

http://www.itiger.me/?p=38

http://blog.leezhong.com/ios/2013/12/27/reactivecocoa-2.html

https://github.com/ReactiveCocoa/ReactiveCocoa/blob/master/Documentation/FrameworkOverview.md

http://www.haskell.org/haskellwiki/Functional_Reactive_Programming

http://blog.zhaojie.me/2009/09/functional-reactive-programming-for-csharp.html

代码

https://github.com/Machx/MVVM-IOS-Example

https://github.com/ReactiveCocoa/RACiOSDemo

书籍

https://leanpub.com/iosfrp

视频

http://vimeo.com/65637501

配置RAC环境
platform:ios, '8.0'
pod 'ReactiveCocoa'
RAC正式学习
// RACSignal使用步骤:
// 1.创建信号 
+ (RACSignal *)createSignal:(RACDisposable * (^)(id<RACSubscriber> subscriber))didSubscribe
// 2.订阅信号,才会激活信号.
 - (RACDisposable *)subscribeNext:(void (^)(id x))nextBlock
// 3.发送信号 
- (void)sendNext:(id)value
OC代码
- (IBAction)testBtn:(id)sender {
   
#pragma mark - RAC监听tf中文字改变
    [[self.testTF rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
        NSLog(@"change: %@", x);
    }];
//    对于textFild的文字更改监听也有更简单的写法
    [[self.testTF rac_textSignal] subscribeNext:^(id x) {
        
    }];
    
#pragma mark - RAC监听手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
       // 监听手势
    }];
    
#pragma mark - RAC监听代理
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"sure" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil];
    /**
     *  @selector 是指这次事件监听的方法
     *
     *  fromProtocol指依赖的代理
     *  
     *  tuple.second是ButtonAtIndex中Button的序号
     *
     *  @return
     */
    [[self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)] subscribeNext:^(RACTuple *tuple) {
        NSLog(@"%@", tuple);
    }];
    [alertView show];
    
    
#pragma mark - 监听通知
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"liyang" object:nil] subscribeNext:^(NSNotification *sender) {
        // 当监听到名字是liyang的通知,会执行block里面的方法
        // RAC中的通知不需要remove observer,因为在rac_add方法中他已经写了remove
    }];
    
#pragma mark - 监听KVO
    UIScrollView *scrolView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];
    scrolView.contentSize = CGSizeMake(200, 800);
    scrolView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:scrolView];
    [RACObserve(scrolView, contentOffset) subscribeNext:^(id x) {
        NSLog(@"success");
        // RACObserve(TARGET, KEYPATH)这种形式,TARGET是监听目标,KEYPATH是要观察的属性值
    }];
    
}
上一篇 下一篇

猜你喜欢

热点阅读