美文网首页
BlocksKit UITapGestureRecognizer

BlocksKit UITapGestureRecognizer

作者: alex_zn | 来源:发表于2018-07-26 15:31 被阅读0次

    BlocksKit UITapGestureRecognizer

    按钮绑定
    // button 点击事件绑定
    btn.btn.bk_(whenTapped: {  
        print("----")
    })
    
    添加Tap手势
    // 单次点击
    - (void)bk_whenTapped:(void (^)(void))block
    {
        [self bk_whenTouches:1 tapped:1 handler:block];
    }
    // 点击回调
    - (void)bk_whenTouches:(NSUInteger)numberOfTouches tapped:(NSUInteger)numberOfTaps handler:(void (^)(void))block
    {
        if (!block) return;
        UITapGestureRecognizer *gesture = [UITapGestureRecognizer bk_recognizerWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
            if (state == UIGestureRecognizerStateRecognized) block();
        }];
        [self addGestureRecognizer:gesture];
    }
    
    bk_recognizerWithHandler

    通过对UIGestureRecognizer的分类动态添加

    // 添加手势入口
    + (id)bk_recognizerWithHandler:(void (^)(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location))block
    {
        return [self bk_recognizerWithHandler:block delay:0.0];
    }
    // 进一步拆分 bk_recognizerWithHandler
    + (id)bk_recognizerWithHandler:(void (^)(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location))block delay:(NSTimeInterval)delay
    {
        return [[[self class] alloc] bk_initWithHandler:block delay:delay];
    }
    // 统一初始化入口 bk_initWithHandler
    - (id)bk_initWithHandler:(void (^)(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location))block delay:(NSTimeInterval)delay
    {
        self = [self initWithTarget:self action:@selector(bk_handleAction:)];
        if (!self) return nil;
    
        self.bk_handler = block;
        self.bk_handlerDelay = delay;
    
        return self;
    }
    
    // bk_handleAction 通过添加的bk_handler变量,点击按钮时,实例化block
    - (void)bk_handleAction:(UIGestureRecognizer *)recognizer
    {
        
        // bk_handler 通过AssociatedObject关联对象 添加的成员 setter 调用bk_setHandler方法赋值
        /*
        @property (nonatomic, copy, setter = bk_setHandler:) void (^bk_handler)(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location);
     
        - (void)bk_setHandler:(void (^)(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location))handle {
            objc_setAssociatedObject(self, BKGestureRecognizerBlockKey, handler, OBJC_ASSOCIATION_COPY_NONATOMIC);}
        */
        void (^handler)(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) = recognizer.bk_handler;
        if (!handler) return;
        
        NSTimeInterval delay = self.bk_handlerDelay;
        CGPoint location = [self locationInView:self.view];
        void (^block)(void) = ^{
            if (!self.bk_shouldHandleAction) return;
            handler(self, self.state, location);
        };
    
        self.bk_shouldHandleAction = YES;
    
        if (!delay) {
            block();
            return;
        }
    
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), block);
    }
    

    bk_handler

    // 声明成员变量的引用
    // static const void *BKGestureRecognizerBlockKey = &BKGestureRecognizerBlockKey;
    
    // 属性声明
    @property (nonatomic, copy, setter = bk_setHandler:) void (^bk_handler)(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location);
    
    // setter方法
    - (void)bk_setHandler:(void (^)(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location))handler {
        objc_setAssociatedObject(self, BKGestureRecognizerBlockKey, handler, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    

    相关文章

      网友评论

          本文标题:BlocksKit UITapGestureRecognizer

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