Repository navigation
Expand file tree
/
Copy pathButton.m
More file actions
executable file
·149 lines (131 loc) · 5.84 KB
/
Copy pathButton.m
File metadata and controls
executable file
·149 lines (131 loc) · 5.84 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// Button.m
// PlainText
//
// Created by Jesse Grosjean on 6/23/10.
//
#import "Button.h"
#import "ApplicationViewController.h"
#import "ApplicationController.h"
#import "PressIndicatorView.h"
#import "UIImage_Additions.h"
@implementation Button
+ (Button *)buttonWithImage:(UIImage *)image color:(UIColor *)color accessibilityLabel:(NSString *)accessibilityLabel accessibilityHint:(NSString *)accessibilityHint target:(id)target action:(SEL)action edgeInsets:(UIEdgeInsets)edgeInsets {
Button *button = [Button buttonWithType:UIButtonTypeCustom];
button.accessibilityLabel = accessibilityLabel;
button.accessibilityHint = accessibilityHint;
image = [UIImage colorizeImage:image color:color];
button.imageEdgeInsets = edgeInsets;
CGSize size = image.size;
button.frame = CGRectMake(0, 0, edgeInsets.left + edgeInsets.right + size.width, edgeInsets.top + edgeInsets.bottom + size.height);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:button action:@selector(pressGrow) forControlEvents:UIControlEventTouchDown];
[button addTarget:button action:@selector(pressGrow) forControlEvents:UIControlEventTouchDragEnter];
[button addTarget:button action:@selector(pressShrink) forControlEvents:UIControlEventTouchDragExit];
[button addTarget:button action:@selector(pressShrink) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return button;
}
+ (Button *)buttonWithImage:(UIImage *)image accessibilityLabel:(NSString *)accessibilityLabel accessibilityHint:(NSString *)accessibilityHint target:(id)target action:(SEL)action edgeInsets:(UIEdgeInsets)edgeInsets {
Button *button = [Button buttonWithType:UIButtonTypeCustom];
button.accessibilityLabel = accessibilityLabel;
button.accessibilityHint = accessibilityHint;
image = [UIImage colorizeImage:image color:[APP_VIEW_CONTROLLER inkColor]];
button.imageEdgeInsets = edgeInsets;
CGSize size = image.size;
button.frame = CGRectMake(0, 0, edgeInsets.left + edgeInsets.right + size.width, edgeInsets.top + edgeInsets.bottom + size.height);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:button action:@selector(pressGrow) forControlEvents:UIControlEventTouchDown];
[button addTarget:button action:@selector(pressGrow) forControlEvents:UIControlEventTouchDragEnter];
[button addTarget:button action:@selector(pressShrink) forControlEvents:UIControlEventTouchDragExit];
[button addTarget:button action:@selector(pressShrink) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return button;
}
+ (Button *)buttonWithTitle:(NSString *)title target:(id)target action:(SEL)action edgeInsets:(UIEdgeInsets)edgeInsets {
Button *button = [Button buttonWithType:UIButtonTypeCustom];
button.titleEdgeInsets = edgeInsets;
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:button action:@selector(pressGrow) forControlEvents:UIControlEventTouchDown];
[button addTarget:button action:@selector(pressGrow) forControlEvents:UIControlEventTouchDragEnter];
[button addTarget:button action:@selector(pressShrink) forControlEvents:UIControlEventTouchDragExit];
[button addTarget:button action:@selector(pressShrink) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (void)setImage:(UIImage *)image forState:(UIControlState)state {
image = [UIImage colorizeImage:image color:[APP_VIEW_CONTROLLER inkColor]];
[super setImage:image forState:state];
}
- (void)refreshFromDefaults {
if (brightness == 0) {
brightness = 1.0;
}
UIImage *image = [self imageForState:UIControlStateNormal];
image = [UIImage colorizeImage:image color:[APP_VIEW_CONTROLLER inkColorByPercent:brightness]];
[self setImage:image forState:UIControlStateNormal];
[self setTitleColor:[APP_VIEW_CONTROLLER inkColor] forState:UIControlStateNormal];
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
if (newSuperview) {
[self refreshFromDefaults];
}
[super willMoveToSuperview:newSuperview];
}
/*- (id)init { never getting called
self = [super init];
brightness = 0.8;
[self refreshFromDefaults];
[self sizeToFit];
UIEdgeInsets reverseInsets = self.titleEdgeInsets;
reverseInsets.top *= -1;
reverseInsets.bottom *= -1;
reverseInsets.left *= -1;
reverseInsets.right *= -1;
self.frame = UIEdgeInsetsInsetRect(self.frame, reverseInsets);
return self;
}*/
@synthesize brightness;
- (void)setBrightness:(CGFloat)aFloat {
brightness = aFloat;
[self refreshFromDefaults];
}
- (void)setImage:(UIImage *)image {
[self setImage:image forState:UIControlStateNormal];
}
- (void)pressGrow {
UIWindow *window = self.window;
UIView *pressIndicator = [window viewWithTag:35];
if (!pressIndicator) {
pressIndicator = [[[PressIndicatorView alloc] init] autorelease];
pressIndicator.userInteractionEnabled = NO;
pressIndicator.tag = 35;
pressIndicator.alpha = 0.0;
[window addSubview:pressIndicator];
}
CGRect bounds = UIEdgeInsetsInsetRect(self.bounds, self.imageEdgeInsets);
CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
center = [self convertPoint:center toView:nil];
pressIndicator.center = center;
[UIView beginAnimations:@"pressIndicatorShow" context:NULL];
[UIView setAnimationsEnabled:YES];
[UIView setAnimationDuration:0.1];
pressIndicator.alpha = 1.0;
[UIView commitAnimations];
}
- (void)pressShrink {
UIView *pressIndicator = [[self window] viewWithTag:35];
[UIView beginAnimations:@"pressIndicatorHide" context:NULL];
[UIView setAnimationsEnabled:YES];
[UIView setAnimationDuration:0.1];
pressIndicator.alpha = 0.0;
[UIView commitAnimations];
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
if (DEBUG_DRAWING) {
[[UIColor blueColor] set];
UIRectFrame(self.bounds);
}
}
@end