-
Notifications
You must be signed in to change notification settings - Fork 2
/
GXGesturedButton.m
108 lines (81 loc) · 3.32 KB
/
GXGesturedButton.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// GXGesturedButton.m
// Thueyts
//
// Created by Mathieu Godart on 09/07/10.
// Copyright 2010 Cogetic. All rights reserved.
//
#import "GXGesturedButton.h"
@implementation GXGesturedButton
@synthesize target;
/*
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
}
return self;
}
*/
- (void)addAction:(SEL)action forControlEvent:(GXGesturedButtonEvent)controlEvent {
UIGestureRecognizer *recognizer = nil;
switch (controlEvent) {
case GXGesturedButtonEventSwipeUp:
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:target action:action];
((UISwipeGestureRecognizer *)recognizer).direction = UISwipeGestureRecognizerDirectionUp;
[self addGestureRecognizer:recognizer];
swipeUpAction = action;
break;
case GXGesturedButtonEventSwipeDown:
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:target action:action];
((UISwipeGestureRecognizer *)recognizer).direction = UISwipeGestureRecognizerDirectionDown;
[self addGestureRecognizer:recognizer];
swipeDownAction = action;
break;
case GXGesturedButtonEventSwipeRight:
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:target action:action];
((UISwipeGestureRecognizer *)recognizer).direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:recognizer];
swipeRightAction = action;
break;
case GXGesturedButtonEventSwipeLeft:
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:target action:action];
((UISwipeGestureRecognizer *)recognizer).direction = UISwipeGestureRecognizerDirectionLeft;
[self addGestureRecognizer:recognizer];
swipeLeftAction = action;
break;
case GXGesturedButtonEventTapTwoFingers:
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
((UITapGestureRecognizer *)recognizer).numberOfTouchesRequired = 2;
[self addGestureRecognizer:recognizer];
tapTwoFingersAction = action;
break;
case GXGesturedButtonEventTapHalfUp:
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self addGestureRecognizer:recognizer];
tapHalfUpAction = action;
break;
case GXGesturedButtonEventTapHalfDown:
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self addGestureRecognizer:recognizer];
tapHalfDownAction = action;
break;
default:
return;
}
[recognizer release];
}
- (void)handleTap:(UITapGestureRecognizer *)sender {
CGPoint touchLocation = [sender locationInView:self];
if (touchLocation.y < (self.frame.size.height / 2)) {
// Upper half.
[self sendAction:tapHalfUpAction to:target forEvent:nil];
}
else {
// Second half.
[self sendAction:tapHalfDownAction to:target forEvent:nil];
}
}
- (void)dealloc {
[super dealloc];
[target release];
}
@end