IOS-uiview add Click Event

Source: Internet
Author: User
========================================================== =============================== Original blog, reprinted please declare the source of Electronic coffee (original id blue rock) ========================================================== ================================ the IOS view does not have the click function, another big difference between Android and Android is that we can't customize one. We wanted to write it by ourselves. After a visit to GitHub, we found that some of our colleagues have already written it, I don't need to repeat it either. Here I just record it: GitHub project connection: GitHub
#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
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.