-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFGTouchableView.m
71 lines (54 loc) · 1.97 KB
/
FGTouchableView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// FGTouchableView.m
// train-helper
//
// Created by 易元 白 on 15/3/17.
// Copyright (c) 2015年 eldereal. All rights reserved.
//
#import "FGTouchableView.h"
#import "FGDecorationViewContext.h"
#import <objc/runtime.h>
@interface UIView(FGTouchableView)
@property (nonatomic, strong) FGVoidBlockHolder *touchableCallback;
@property (nonatomic, strong) UIGestureRecognizer *touchGestureRecognizer;
@end
@implementation FastGui(FGTouchableView)
+ (void)beginTouchableViewsWithOnTouchBlock:(FGVoidBlock)onTouch
{
[FastGui beginDecorationContextWithBlock:^(UIView *view) {
if (view.touchableCallback == nil) {
view.touchableCallback = [FGVoidBlockHolder holderWithBlock: onTouch];
}else{
view.touchableCallback.block = onTouch;
}
if (view.touchGestureRecognizer == nil) {
view.touchGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:view.touchableCallback action:@selector(notify)];
[view addGestureRecognizer: view.touchGestureRecognizer];
}
}];
}
+ (void)endTouchableViews
{
[FastGui endDecorationContext];
}
@end
static void * TouchableCallbackPropertyKey = &TouchableCallbackPropertyKey;
static void * TouchGestureRecognizerPropertyKey = &TouchGestureRecognizerPropertyKey;
@implementation UIView(FGTouchableView)
- (FGVoidBlockHolder *)touchableCallback
{
return objc_getAssociatedObject(self, TouchableCallbackPropertyKey);
}
- (void)setTouchableCallback:(FGVoidBlockHolder *)touchableCallback
{
objc_setAssociatedObject(self, TouchableCallbackPropertyKey, touchableCallback, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIGestureRecognizer *)touchGestureRecognizer
{
return objc_getAssociatedObject(self, TouchGestureRecognizerPropertyKey);
}
- (void)setTouchGestureRecognizer:(UIGestureRecognizer *)touchGestureRecognizer
{
objc_setAssociatedObject(self, TouchGestureRecognizerPropertyKey, touchGestureRecognizer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end