// // UIView+gesture.m // // Created by CHD on 2018/5/8. // Copyright © 2018年 chd. All rights reserved. // #import "UIView+gesture.h" #import static char kDTActionHandlerTapBlockKey; static char kDTActionHandlerTapGestureKey; static char kDTActionHandlerLongPressBlockKey; static char kDTActionHandlerLongPressGestureKey; @implementation UIView (gesture) - (void)setTapActionWithBlock:(void (^)(UITapGestureRecognizer*tap))block { self.userInteractionEnabled = YES; 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)(UITapGestureRecognizer*tap) = objc_getAssociatedObject(self, &kDTActionHandlerTapBlockKey); if (action) { action(gesture); } } } - (void)setLongPressActionWithBlock:(void (^)(UILongPressGestureRecognizer*longP))block { self.userInteractionEnabled = YES; 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)(UILongPressGestureRecognizer*longP) = objc_getAssociatedObject(self, &kDTActionHandlerLongPressBlockKey); if (action) { action((UILongPressGestureRecognizer*)gesture); } } } @end