iOS 开发实用技术导航
NSHipster 中文版
› http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
› http://www.cocos2d-iphone.org/
CocoaPods
› http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
› http://code.google.com/mobile/analytics/
WWDC
› https://developer.apple.com/wwdc/
Design Guides and Resources
› https://developer.apple.com/design/
Transcripts of WWDC sessions
› http://asciiwwdc.com
Cocoa with Love
› http://cocoawithlove.com/
Cocoa Dev Central
› http://cocoadevcentral.com/
NSHipster
› http://nshipster.com/
Style Guides
› Google Objective-C Style Guide
› NYTimes Objective-C Style Guide
Useful Tools and Services
› Charles Web Debugging Proxy
› Smore
aaaron7
V2EX  ›  iDev

撸了个轮子,集成 GestureRecognizer 到 ReactiveCocoa 里

  •  
  •   aaaron7 ·
    aaaron7 · Jan 16, 2016 · 3731 views
    This topic created in 3904 days ago, the information mentioned may be changed or developed.

    最近用 ReactiveCocoa ,相见恨晚。不过遗憾的是没有提供一个添加 Gesture Recognizer 的接口。看了一下 RAC 提供的其他 Category 的实现,感觉实现起来也不难,于是就撸了一个。

    没啥技术含量,但用起来真的相当方便。

    传送门:RAC-GestureRecognizer

    常见用法:

    //添加点击的处理事件
    [[self.view rac_signalForGestureRecognizer:[UITapGestureRecognizer class]] subscribeNext:^(UITapGestureRecognizer * x) {
        NSLog(@"view tapped at %f,%f", [x locationInView:x.view].x,[x locationInView:x.view].y);
    }];
    
    //添加仅针对 View 中特定区域的拖动检测
    [[[self.view rac_signalForGestureRecognizer:[UIPanGestureRecognizer class]] filter:^BOOL(id value) {
        UIPanGestureRecognizer* pgr = (UIPanGestureRecognizer*)value;
        CGRect rect = CGRectMake(100, 100, 200, 200);
        return CGRectContainsPoint(rect, [pgr locationInView:pgr.view]);
    }] subscribeNext:^(id x) {
        NSLog(@"panned in given rect");
    }];
    
    
    //缩放事件
    [[self.view rac_signalForGestureRecognizer:[UIPinchGestureRecognizer class]] subscribeNext:^(id x) {
        NSLog(@"view pinched");
    }];
    
    
    
    //可拖拽 view 的一个非常简单的实现方式
    UIView* t = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    t.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:t];
    
    RAC(t,center) = [[t rac_signalForGestureRecognizer:[UIPanGestureRecognizer class]] map:^id(UIPanGestureRecognizer* value) {
        CGPoint p = [value translationInView:self.view];
        [value setTranslation:CGPointZero inView:self.view];
        return [NSValue valueWithCGPoint:CGPointMake(t.center.x + p.x, t.center.y + p.y)];
    }];
    

    PS

    • 因为拓展方法返回的是 RACSignal ,所以可以结合 RAC 中的各种犀利的操作符实现各种犀利的逻辑
    • 需要结合 RAC 2.x 一起使用。
    • 其他的见 github 的说明
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2294 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 00:55 · PVG 08:55 · LAX 17:55 · JFK 20:55
    ♥ Do have faith in what you're doing.