V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
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 · 2016-01-16 14:52:30 +08:00 · 2627 次点击
    这是一个创建于 3016 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近用 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 的说明
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   863 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 21:56 · PVG 05:56 · LAX 14:56 · JFK 17:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.