-
Notifications
You must be signed in to change notification settings - Fork 1
/
JFWRepeatingButton.m
99 lines (76 loc) · 2.03 KB
/
JFWRepeatingButton.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
//
// JFWRepeatingButton.m
//
// Created by Julian Weinert on 12/03/15.
// Licensed under the WTFPL.
//
#import "JFWRepeatingButton.h"
@interface JFWRepeatingButton ()
@property (nonatomic, retain) NSTimer *delayTimer;
@property (nonatomic, retain) NSTimer *repeatingTimer;
@property (nonatomic, retain) NSTimer *accellerationTimer;
@property (atomic, assign) BOOL eventReactionPaused;
@end
@implementation JFWRepeatingButton
+ (id)buttonWithType:(UIButtonType)buttonType {
return [[self alloc] init];
}
- (instancetype)init {
if (self = [super init]) {
[self setup];
}
return self;
}
- (void)awakeFromNib {
[self setup];
}
- (void)setup {
[self addTarget:self action:@selector(clickBegan:) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(clickEnded) forControlEvents:UIControlEventTouchUpInside];
}
- (void)startRepeating {
[[self delayTimer] invalidate];
_delayTimer = nil;
_repeatingTimer = [NSTimer scheduledTimerWithTimeInterval:[self repeatInterval]
target:self
selector:@selector(repeat)
userInfo:nil
repeats:YES];
}
- (void)stopRepeating {
[[self delayTimer] invalidate];
_delayTimer = nil;
[[self repeatingTimer] invalidate];
_repeatingTimer = nil;
}
- (void)repeat {
_eventReactionPaused = YES;
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
_eventReactionPaused = NO;
}
- (void)clickBegan:(id)sender {
[self stopRepeating];
if ([self repeatInterval] > 0) {
_delayTimer = [NSTimer scheduledTimerWithTimeInterval:[self threshhold]
target:self
selector:@selector(startRepeating)
userInfo:nil
repeats:NO];
}
if ([self accellerationInterval] > 0) {
}
}
- (void)clickEnded {
if ([self eventReactionPaused]) {
return;
}
[self stopRepeating];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end