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

直接贴代码
需要的朋友直接创建一个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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 重点参考链接: View Programming Guide for iOS https://developer....
    Kevin_Junbaozi阅读 4,515评论 0 15
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,144评论 1 32
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • 这段时间,有隐约的感觉自己是不是太庸了,太碌碌无为了,实在是没有任何的亮点。竟然能如此心安理得的玩到这么大,也是惭...
    一个小疯子阅读 172评论 0 0
  • 谁的心驶过无边际的荒野? 远处传来火车鸣笛声。 即将归家的燕啊, 你连带着故乡飘忽的云。 再也看不到绵延的河流, ...
    啾悦阅读 249评论 0 0