你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS---UIView添加點擊事件

IOS---UIView添加點擊事件

編輯:IOS開發綜合

IOS的view沒有點擊功能,又是和Android 的一大差別,沒辦法,自定義一個,本來是想自己寫一個的,逛了一下github,發現有同行已經寫好了,我也不需要在重復了,這裡只是記錄一下吧:
 這裡我們為UIViuew定義了Category,如下:
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 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved