forked from nihalsharma/Clock-Bar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTouchButton.m
More file actions
90 lines (69 loc) · 2 KB
/
Copy pathTouchButton.m
File metadata and controls
90 lines (69 loc) · 2 KB
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
#import <Cocoa/Cocoa.h>
#import "TouchButton.h"
static double LONG_PRESS_TIME = 0.5;
@interface TouchButton ()
@property double touchBeganTime;
@end
@implementation TouchButton
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (void)touchesBeganWithEvent:(NSEvent *)event
{
NSSet<NSTouch *> *touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:self];
NSTouch *touch = touches.anyObject;
if (touch != nil)
{
if (touch.type == NSTouchTypeDirect)
{
self.touchBeganTime = [[NSDate date] timeIntervalSince1970];
}
}
[super touchesBeganWithEvent:event];
}
- (void)touchesMovedWithEvent:(NSEvent *)event
{
for (NSTouch *touch in [event touchesMatchingPhase:NSTouchPhaseMoved inView:self])
{
if (touch.type == NSTouchTypeDirect)
{
break;
}
}
[super touchesMovedWithEvent:event];
}
- (void)touchesEndedWithEvent:(NSEvent *)event
{
for (NSTouch *touch in [event touchesMatchingPhase:NSTouchPhaseEnded inView:self])
{
if (touch.type == NSTouchTypeDirect)
{
if(self.delegate != nil)
{
double touchTime = [[NSDate date] timeIntervalSince1970] - self.touchBeganTime;
if(touchTime >= LONG_PRESS_TIME) {
[self.delegate onLongPressed: self];
}
else
{
[self.delegate onPressed: self];
}
}
break;
}
}
[super touchesEndedWithEvent:event];
}
- (void)touchesCancelledWithEvent:(NSEvent *)event
{
for (NSTouch *touch in [event touchesMatchingPhase:NSTouchPhaseMoved inView:self])
{
if (touch.type == NSTouchTypeDirect)
{
break;
}
}
[super touchesCancelledWithEvent:event];
}
@end