美文网首页
UIView 快速添加手势事件(runtime)

UIView 快速添加手势事件(runtime)

作者: ivylee_mr | 来源:发表于2018-12-28 11:38 被阅读2次

    直接贴代码
    需要的朋友直接创建一个UIView 的分类 复制粘贴即可

    #import <UIKit/UIKit.h>
    
    
    @interface UIView (UserInteraction)
    
    /**
     添加手势点击事件
    
     @param block 回掉
     */
    - (void)addTapGestureActionWithBlock:(void (^)(UITapGestureRecognizer *tapAction))block;
    
    
    /**
     添加手势长按事件
    
     @param block 回掉
     */
    - (void)addLongPressGestureActionWithBlock:(void (^)(UILongPressGestureRecognizer *longPressAction))block;
    
    @end
    
    //
    //  UIView+UserInteraction.m
    //  FrameWork
    //
    //  Created by LeeMiao on 2017/9/15.
    //  Copyright © 2017年 Limiao. All rights reserved.
    //
    
    #import "UIView+UserInteraction.h"
    #import <objc/runtime.h>
    
    static char const kActionHandlerTapBlockKey;
    static char const kActionHandlerTapGestureKey;
    static char const kActionHandlerLongPressBlockKey;
    static char const kActionHandlerLongPressGestureKey;
    
    @interface UIView ()
    
    @property (nonatomic, strong) NSNumber  *userInteractionNumbers; //
    
    @end
    
    @implementation UIView (UserInteraction)
    
    
    
    #pragma mark - gesture block
    - (void)addTapGestureActionWithBlock:(void (^)(UITapGestureRecognizer *))block{
        UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerTapGestureKey);
        if (!gesture){
            self.userInteractionEnabled = YES;
            gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForTapGesture:)];
            [self addGestureRecognizer:gesture];
            objc_setAssociatedObject(self, &kActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
        }
        objc_setAssociatedObject(self, &kActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY);
    }
    
    - (void)handleActionForTapGesture:(UITapGestureRecognizer *)gesture{
        if (gesture.state == UIGestureRecognizerStateRecognized){
            void(^action)(UITapGestureRecognizer *tapAction) = objc_getAssociatedObject(self, &kActionHandlerTapBlockKey);
            if (action){
                action(gesture);
            }
        }
    }
    
    - (void)addLongPressGestureActionWithBlock:(void (^)(UILongPressGestureRecognizer *))block{
        UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerLongPressGestureKey);
        if (!gesture){
            self.userInteractionEnabled = YES;
            gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForLongPressGesture:)];
            [self addGestureRecognizer:gesture];
            objc_setAssociatedObject(self, &kActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
        }
        objc_setAssociatedObject(self, &kActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY);
    }
    
    - (void)handleActionForLongPressGesture:(UILongPressGestureRecognizer *)gesture{
        if (gesture.state == UIGestureRecognizerStateBegan){
            void(^action)(UILongPressGestureRecognizer *longPressAction) = objc_getAssociatedObject(self, &kActionHandlerLongPressBlockKey);
            if (action){
                action(gesture);
            }
        }
    }
    
    
    
    @end
    

    相关文章

      网友评论

          本文标题:UIView 快速添加手势事件(runtime)

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