1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- //
- // UIView+UIView_BlockGesture.m
- // Created by MacKun on 15/4/25.
- // Copyright (c) 2015年 MacKun All rights reserved.
- //
- #import "UIView+BlockGesture.h"
- #import <objc/runtime.h>
- static char kActionHandlerTapBlockKey;
- static char kActionHandlerTapGestureKey;
- static char kActionHandlerLongPressBlockKey;
- static char kActionHandlerLongPressGestureKey;
- @implementation UIView (BlockGesture)
- - (void)addTapActionWithBlock:(GestureActionBlock)block
- {
- UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerTapGestureKey);
- if (!gesture)
- {
- 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)
- {
- GestureActionBlock block = objc_getAssociatedObject(self, &kActionHandlerTapBlockKey);
- if (block)
- {
- block(gesture);
- }
- }
- }
- - (void)addLongPressActionWithBlock:(GestureActionBlock)block
- {
- UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerLongPressGestureKey);
- if (!gesture)
- {
- 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:(UITapGestureRecognizer*)gesture
- {
- if (gesture.state == UIGestureRecognizerStateRecognized)
- {
- GestureActionBlock block = objc_getAssociatedObject(self, &kActionHandlerLongPressBlockKey);
- if (block)
- {
- block(gesture);
- }
- }
- }
- @end
|