一句话给UIView及其子类添加手势

作者: 强强刘 | 来源:发表于2016-11-23 11:18 被阅读420次

    开发中经常会遇到需要给UIView或者它的子类添加单击或者长按手势

    如果每次添加都去定义手势,add到控件上...也显得非常繁琐,所以今天给大家封装一个一句话添加单击手势的分类

    首先我们的目标是对UIView和他的子类,所以最好的是对UIView写一个分类出来(废话不说了,直接上代码)

    UIView+gesture.h

    #import <UIKit/UIKit.h>
    
    @interface UIView (gesture)
    - (void)setTapActionWithBlock:(void (^)(void))block;
    
    - (void)setLongPressActionWithBlock:(void (^)(void))block;
    @end
    

    UIView+gesture.m

    #import "UIView+gesture.h"
    #import <objc/runtime.h>
    
    static char kDTActionHandlerTapBlockKey;
    static char kDTActionHandlerTapGestureKey;
    static char kDTActionHandlerLongPressBlockKey;
    static char kDTActionHandlerLongPressGestureKey;
    
    @implementation UIView (gesture)
    
    - (void)setTapActionWithBlock:(void (^)(void))block
    {
        UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &kDTActionHandlerTapGestureKey);
        
        if (!gesture)
        {
            gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__handleActionForTapGesture:)];
            [self addGestureRecognizer:gesture];
            objc_setAssociatedObject(self, &kDTActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
        }
        
        objc_setAssociatedObject(self, &kDTActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY);
    }
    
    - (void)__handleActionForTapGesture:(UITapGestureRecognizer *)gesture
    {
        if (gesture.state == UIGestureRecognizerStateRecognized)
        {
            void(^action)(void) = objc_getAssociatedObject(self, &kDTActionHandlerTapBlockKey);
            
            if (action)
            {
                action();
            }
        }
    }
    
    - (void)setLongPressActionWithBlock:(void (^)(void))block
    {
        UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &kDTActionHandlerLongPressGestureKey);
        
        if (!gesture)
        {
            gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(__handleActionForLongPressGesture:)];
            [self addGestureRecognizer:gesture];
            objc_setAssociatedObject(self, &kDTActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
        }
        
        objc_setAssociatedObject(self, &kDTActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY);
    }
    
    - (void)__handleActionForLongPressGesture:(UITapGestureRecognizer *)gesture
    {
        if (gesture.state == UIGestureRecognizerStateBegan)
        {
            void(^action)(void) = objc_getAssociatedObject(self, &kDTActionHandlerLongPressBlockKey);
            
            if (action)
            {
                action();
            }
        }
    }
    
    @end
    

    相关文章

      网友评论

      • donggelaile:感谢分享, 有两个地方可以优化下
        1、设置手势的时候加上self.userInteractionEnabled = YES;
        2、block的返回参数加上getsture,比如tapGesture, - (void)setTapActionWithBlock:(void (^)(UITapGestureRecognizer*tap))block
        强强刘:感谢你的建议
      • yue博客:static char xxx 不用赋值的么?
        强强刘:@yue博客 不用赋值的

      本文标题:一句话给UIView及其子类添加手势

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