============================================================博文原創,轉載請聲明出處電子咖啡(原id藍岩)============================================================IOS的view沒有點擊功能,又是和Android 的一大差別,沒辦法,自訂一個,本來是想自己寫一個的,逛了一下github,發現有同行已經寫好了,我也不需要在重複了,這裡只是記錄一下吧:github工程串連:https://github.com/jakemarsh/JMWhenTapped這裡我們為UIViuew定義了Category,如下:UIView+WhenTappedBlocks.h
#if NS_BLOCKS_AVAILABLE#import <UIKit/UIKit.h>typedef void (^JMWhenTappedBlock)();@interface UIView (JMWhenTappedBlocks) <UIGestureRecognizerDelegate>- (void)whenTapped:(JMWhenTappedBlock)block;- (void)whenDoubleTapped:(JMWhenTappedBlock)block;- (void)whenTwoFingerTapped:(JMWhenTappedBlock)block;- (void)whenTouchedDown:(JMWhenTappedBlock)block;- (void)whenTouchedUp:(JMWhenTappedBlock)block;@end#endif
UIView+WhenTappedBlocks.m
#if NS_BLOCKS_AVAILABLE#import "UIView+WhenTappedBlocks.h"#import <objc/runtime.h>@interface UIView (JMWhenTappedBlocks_Private)- (void)runBlockForKey:(void *)blockKey;- (void)setBlock:(JMWhenTappedBlock)block forKey:(void *)blockKey;- (UITapGestureRecognizer*)addTapGestureRecognizerWithTaps:(NSUInteger) taps touches:(NSUInteger) touches selector:(SEL) selector;- (void) addRequirementToSingleTapsRecognizer:(UIGestureRecognizer*) recognizer;- (void) addRequiredToDoubleTapsRecognizer:(UIGestureRecognizer*) recognizer;@end@implementation UIView (JMWhenTappedBlocks)static char kWhenTappedBlockKey;static char kWhenDoubleTappedBlockKey;static char kWhenTwoFingerTappedBlockKey;static char kWhenTouchedDownBlockKey;static char kWhenTouchedUpBlockKey;#pragma mark -#pragma mark Set blocks- (void)runBlockForKey:(void *)blockKey { JMWhenTappedBlock block = objc_getAssociatedObject(self, blockKey); if (block) block();}- (void)setBlock:(JMWhenTappedBlock)block forKey:(void *)blockKey { self.userInteractionEnabled = YES; objc_setAssociatedObject(self, blockKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);}#pragma mark -#pragma mark When Tapped- (void)whenTapped:(JMWhenTappedBlock)block { UITapGestureRecognizer* gesture = [self addTapGestureRecognizerWithTaps:1 touches:1 selector:@selector(viewWasTapped)]; [self addRequiredToDoubleTapsRecognizer:gesture]; [self setBlock:block forKey:&kWhenTappedBlockKey];}- (void)whenDoubleTapped:(JMWhenTappedBlock)block { UITapGestureRecognizer* gesture = [self addTapGestureRecognizerWithTaps:2 touches:1 selector:@selector(viewWasDoubleTapped)]; [self addRequirementToSingleTapsRecognizer:gesture]; [self setBlock:block forKey:&kWhenDoubleTappedBlockKey];}- (void)whenTwoFingerTapped:(JMWhenTappedBlock)block { [self addTapGestureRecognizerWithTaps:1 touches:2 selector:@selector(viewWasTwoFingerTapped)]; [self setBlock:block forKey:&kWhenTwoFingerTappedBlockKey];}- (void)whenTouchedDown:(JMWhenTappedBlock)block { [self setBlock:block forKey:&kWhenTouchedDownBlockKey];}- (void)whenTouchedUp:(JMWhenTappedBlock)block { [self setBlock:block forKey:&kWhenTouchedUpBlockKey];}#pragma mark -#pragma mark Callbacks- (void)viewWasTapped { [self runBlockForKey:&kWhenTappedBlockKey];}- (void)viewWasDoubleTapped { [self runBlockForKey:&kWhenDoubleTappedBlockKey];}- (void)viewWasTwoFingerTapped { [self runBlockForKey:&kWhenTwoFingerTappedBlockKey];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; [self runBlockForKey:&kWhenTouchedDownBlockKey];}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; [self runBlockForKey:&kWhenTouchedUpBlockKey];}#pragma mark -#pragma mark Helpers- (UITapGestureRecognizer*)addTapGestureRecognizerWithTaps:(NSUInteger)taps touches:(NSUInteger)touches selector:(SEL)selector { UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:selector]; tapGesture.delegate = self; tapGesture.numberOfTapsRequired = taps; tapGesture.numberOfTouchesRequired = touches; [self addGestureRecognizer:tapGesture]; return [tapGesture autorelease];}- (void) addRequirementToSingleTapsRecognizer:(UIGestureRecognizer*) recognizer { for (UIGestureRecognizer* gesture in [self gestureRecognizers]) { if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) { UITapGestureRecognizer* tapGesture = (UITapGestureRecognizer*) gesture; if (tapGesture.numberOfTouchesRequired == 1 && tapGesture.numberOfTapsRequired == 1) { [tapGesture requireGestureRecognizerToFail:recognizer]; } } }}- (void) addRequiredToDoubleTapsRecognizer:(UIGestureRecognizer*) recognizer { for (UIGestureRecognizer* gesture in [self gestureRecognizers]) { if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) { UITapGestureRecognizer* tapGesture = (UITapGestureRecognizer*) gesture; if (tapGesture.numberOfTouchesRequired == 2 && tapGesture.numberOfTapsRequired == 1) { [recognizer requireGestureRecognizerToFail:tapGesture]; } } }}@end#endif