IOS—UIView添加點擊事件

來源:互聯網
上載者:User
============================================================博文原創,轉載請聲明出處電子咖啡(原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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.