IOS --- UIView add Click Event

Source: Internet
Author: User

IOS's view does not have the click function, which is a big difference with Android. There is no way to customize it. I originally wanted to write it myself. I visited github and found that some of my colleagues have already written it, I don't need to repeat it either. Here we just record it:
Here we have defined the Category for UIViuew, as follows:
UIView + WhenTappedBlocks. h

[Cpp]
01. # if NS_BLOCKS_AVAILABLE
02.
03. # import <UIKit/UIKit. h>
04.
05. typedef void (^ JMWhenTappedBlock )();
06.
07. @ interface UIView (JMWhenTappedBlocks) <UIGestureRecognizerDelegate>
08.
09.-(void) whenTapped :( JMWhenTappedBlock) block;
10.-(void) whenDoubleTapped :( JMWhenTappedBlock) block;
11.-(void) whenTwoFingerTapped :( JMWhenTappedBlock) block;
12.-(void) whenTouchedDown :( JMWhenTappedBlock) block;
13.-(void) whenTouchedUp :( JMWhenTappedBlock) block;
14.
15. @ end
16.
17. # endif
# 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

# EndifUIView + WhenTappedBlocks. m
[Cpp]
01. # if NS_BLOCKS_AVAILABLE
02.
03. # import "UIView + WhenTappedBlocks. h"
04. # import <objc/runtime. h>
05.
06. @ interface UIView (JMWhenTappedBlocks_Private)
07.
08.-(void) runBlockForKey :( void *) blockKey;
09.-(void) setBlock :( JMWhenTappedBlock) block forKey :( void *) blockKey;
10.
11.-(UITapGestureRecognizer *) addTapGestureRecognizerWithTaps :( NSUInteger) taps touches :( NSUInteger) touches selector :( SEL) selector;
12.-(void) addRequirementToSingleTapsRecognizer :( UIGestureRecognizer *) recognizer;
13.-(void) addRequiredToDoubleTapsRecognizer :( UIGestureRecognizer *) recognizer;
14.
15. @ end
16.
17. @ implementation UIView (JMWhenTappedBlocks)
18.
19. static char kWhenTappedBlockKey;
20. static char kWhenDoubleTappedBlockKey;
21. static char kWhenTwoFingerTappedBlockKey;
22. static char kWhenTouchedDownBlockKey;
23. static char kWhenTouchedUpBlockKey;
24.
25. # pragma mark-
26. # pragma mark Set blocks
27.
28.-(void) runBlockForKey :( void *) blockKey {
29. JMWhenTappedBlock block = objc_getAssociatedObject (self, blockKey );
30. if (block) block ();
31 .}
32.
33.-(void) setBlock :( JMWhenTappedBlock) block forKey :( void *) blockKey {
34. self. userInteractionEnabled = YES;
35. objc_setAssociatedObject (self, blockKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC );
36 .}
37.
38. # pragma mark-
39. # pragma mark When Tapped
40.
41.-(void) whenTapped :( JMWhenTappedBlock) block {
42. UITapGestureRecognizer * gesture = [self addTapGestureRecognizerWithTaps: 1 touches: 1 selector: @ selector (viewWasTapped)];
43. [self addRequiredToDoubleTapsRecognizer: gesture];
44.
45. [self setBlock: block forKey: & kWhenTappedBlockKey];
46 .}
47.
48.-(void) whenDoubleTapped :( JMWhenTappedBlock) block {
49. UITapGestureRecognizer * gesture = [self addTapGestureRecognizerWithTaps: 2 touches: 1 selector: @ selector (viewWasDoubleTapped)];
50. [self addRequirementToSingleTapsRecognizer: gesture];
51.
52. [self setBlock: block forKey: & kWhenDoubleTappedBlockKey];
53 .}
54.
55.-(void) whenTwoFingerTapped :( JMWhenTappedBlock) block {
56. [self addTapGestureRecognizerWithTaps: 1 touches: 2 selector: @ selector (viewWasTwoFingerTapped)];
57.
58. [self setBlock: block forKey: & kWhenTwoFingerTappedBlockKey];
59 .}
60.
61.-(void) whenTouchedDown :( JMWhenTappedBlock) block {
62. [self setBlock: block forKey: & kWhenTouchedDownBlockKey];
63 .}
64.
65.-(void) whenTouchedUp :( JMWhenTappedBlock) block {
66. [self setBlock: block forKey: & kWhenTouchedUpBlockKey];
67 .}
68.
69. # pragma mark-
70. # pragma mark Callbacks
71.
72.-(void) viewWasTapped {
73. [self runBlockForKey: & kWhenTappedBlockKey];
74 .}
75.
76.-(void) viewWasDoubleTapped {
77. [self runBlockForKey: & kWhenDoubleTappedBlockKey];
78 .}
79.
80.-(void) viewWasTwoFingerTapped {
81. [self runBlockForKey: & kWhenTwoFingerTappedBlockKey];
82 .}
83.
84.-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {
85. [super touchesBegan: touches withEvent: event];
86. [self runBlockForKey: & kWhenTouchedDownBlockKey];
87 .}
88.
89.-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {
90. [super touchesEnded: touches withEvent: event];
91. [self runBlockForKey: & kWhenTouchedUpBlockKey];
92 .}
93.
94. # pragma mark-
95. # pragma mark Helpers
96.
97.-(UITapGestureRecognizer *) addTapGestureRecognizerWithTaps :( NSUInteger) taps touches :( NSUInteger) touches selector :( SEL) selector {
98. UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: self action: selector];
99. tapGesture. delegate = self;
100. tapGesture. numberOfTapsRequired = taps;
101. tapGesture. numberOfTouchesRequired = touches;
102. [self addGestureRecognizer: tapGesture];
103.
104. return [tapGesture autorelease];
105 .}
106.
107.-(void) addRequirementToSingleTapsRecognizer :( UIGestureRecognizer *) recognizer {
108. for (UIGestureRecognizer * gesture in [self gestureRecognizers]) {
109. if ([gesture isKindOfClass: [UITapGestureRecognizer class]) {
110. UITapGestureRecognizer * tapGesture = (UITapGestureRecognizer *) gesture;
111. if (tapGesture. numberOfTouchesRequired = 1 & tapGesture. numberOfTapsRequired = 1 ){
112. [tapGesture requireGestureRecognizerToFail: recognizer];
113 .}
114 .}
115 .}
116 .}
117.
118.-(void) addRequiredToDoubleTapsRecognizer :( UIGestureRecognizer *) recognizer {
119. for (UIGestureRecognizer * gesture in [self gestureRecognizers]) {
120. if ([gesture isKindOfClass: [UITapGestureRecognizer class]) {
121. UITapGestureRecognizer * tapGesture = (UITapGestureRecognizer *) gesture;
122. if (tapGesture. numberOfTouchesRequired = 2 & tapGesture. numberOfTapsRequired = 1 ){
123. [recognizer requireGestureRecognizerToFail: tapGesture];
124 .}
125 .}
126 .}
127 .}
128.
129. @ end
130.
131. # endif

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.

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.