This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BLCAwesomeFloatingToolbar.m
190 lines (144 loc) · 6.91 KB
/
BLCAwesomeFloatingToolbar.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// BLCAwesomeFloatingToolbar.m
// BlocBrowser
//
// Created by Douglas Hewitt on 4/22/15.
// Copyright (c) 2015 Bloc. All rights reserved.
//
#import "BLCAwesomeFloatingToolbar.h"
@interface BLCAwesomeFloatingToolbar ()
@property (nonatomic, strong) NSArray *currentTitles;
@property (nonatomic, strong) NSArray *colors;
@property (nonatomic, strong) NSArray *buttons;
@property (nonatomic, weak) UIButton *currentButton;
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
@property (nonatomic, strong) UIPanGestureRecognizer *panGesture;
@property (nonatomic, strong) UIPinchGestureRecognizer *pinchGesture;
@property (nonatomic, strong) UILongPressGestureRecognizer *longPressGesture;
@end
@implementation BLCAwesomeFloatingToolbar
- (instancetype) initWithFourTitles:(NSArray *)titles {
// call superclass initializer to make sure we do all that setup first
self = [super init];
// Save the titles, and set the 4 colors
if (self) {
self.currentTitles = titles;
self.colors = @[[UIColor colorWithRed:199/255.0 green:158/255.0 blue:203/255.0 alpha:1],
[UIColor colorWithRed:255/255.0 green:105/255.0 blue:97/255.0 alpha:1],
[UIColor colorWithRed:222/255.0 green:165/255.0 blue:164/255.0 alpha:1],
[UIColor colorWithRed:255/255.0 green:179/255.0 blue:71/255.0 alpha:1]];
NSMutableArray *buttonsArray = [[NSMutableArray alloc] init];
// Make the 4 buttons
for (NSString *currentTitle in self.currentTitles) {
UIButton *button = [[UIButton alloc] init];
button.userInteractionEnabled = NO;
button.alpha = 0.25;
NSUInteger currentTitleIndex = [self.currentTitles indexOfObject:currentTitle]; // 0 through 3
NSString *titleForThisButton = [self.currentTitles objectAtIndex:currentTitleIndex];
UIColor *colorForThisButton = [self.colors objectAtIndex:currentTitleIndex];
[button setTitle:(NSString *)titleForThisButton forState:UIControlStateNormal];
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.titleLabel.font = [UIFont systemFontOfSize:10];
//button.titleLabel.text = titleForThisButton;
button.backgroundColor = colorForThisButton;
button.titleLabel.textColor = [UIColor whiteColor];
[buttonsArray addObject:button];
}
self.buttons = buttonsArray;
for (UIButton *thisButton in self.buttons) {
[self addSubview:thisButton];
[thisButton addTarget:self action:@selector(tapFired:) forControlEvents:UIControlEventTouchUpInside];
}
self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panFired:)];
[self addGestureRecognizer:self.panGesture];
self.pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchFired:)];
[self addGestureRecognizer:self.pinchGesture];
self.longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressFired:)];
[self addGestureRecognizer:self.longPressGesture];
}
return self;
}
- (void) tapFired:(UIButton *)buttonTap {
if ([self.buttons containsObject:buttonTap]) {
if ([self.delegate respondsToSelector:@selector(floatingToolbar:didSelectButtonWithTitle:)]) {
[self.delegate floatingToolbar:self didSelectButtonWithTitle:((UIButton *)buttonTap).titleLabel.text];
}
}
}
- (void) panFired:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:self];
NSLog(@"New Translation: %@", NSStringFromCGPoint(translation));
if ([self.delegate respondsToSelector:@selector(floatingToolbar:didTryToPanWithOffset:)]) {
[self.delegate floatingToolbar:self didTryToPanWithOffset:translation];
}
[recognizer setTranslation:CGPointZero inView:self];
}
}
- (void) pinchFired:(UIPinchGestureRecognizer *)recognizer {
if (recognizer.state ==UIGestureRecognizerStateChanged) {
CGFloat currentScale = recognizer.scale;
NSLog(@"New Scale: %f", currentScale);
if ([self.delegate respondsToSelector:@selector(floatingToolbar:didTryToPinchWithScaler:)]) {
[self.delegate floatingToolbar:self didTryToPinchWithScaler:currentScale];
}
}
}
- (void) colorChooser {
self.colors = @[self.colors[3],self.colors[0],self.colors[1],self.colors[2]];
for (int i = 0; i<self.buttons.count; i++) {
UIButton *button = self.buttons[i];
UIColor *colorForThisButton = self.colors[i];
button.backgroundColor = colorForThisButton;
}
}
- (void) longPressFired:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateRecognized) {
if ([self.delegate respondsToSelector:@selector(floatingToolbarDidLongTapButton:)]){
[self.delegate floatingToolbarDidLongTapButton:self];
}
}
}
- (void) layoutSubviews {
// set the frames for the 4 buttons
for (UIButton *thisButton in self.buttons) {
NSUInteger currentButtonIndex = [self.buttons indexOfObject:thisButton];
CGFloat buttonHeight = CGRectGetHeight(self.bounds) / 2;
CGFloat buttonWidth = CGRectGetWidth(self.bounds) / 2;
CGFloat buttonX = 0;
CGFloat buttonY = 0;
//adjust buttonX and buttonY for each button
if (currentButtonIndex < 2) {
// 0 or 1, so on the top
buttonY = 0;
} else {
// 2 or 3 so on bottom
buttonY = CGRectGetHeight(self.bounds) / 2;
}
if (currentButtonIndex % 2 == 0) { // is currentButtonIndex evenly divisible by 2?
// 0 or 2, so on the left
buttonX = 0;
} else {
// 1 or 3, so on the right
buttonX = CGRectGetWidth(self.bounds) / 2;
}
thisButton.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
}
}
#pragma mark - Touch Handling
- (UIButton *) buttonFromTouches:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
UIView *subView = [self hitTest:location withEvent:event];
return (UIButton *)subView;
}
#pragma mark - Button Enabling
- (void) setEnabled:(BOOL)enabled forButtonWithTitle:(NSString *)title {
NSUInteger index = [self.currentTitles indexOfObject:title];
if (index != NSNotFound) {
UIButton *button = [self.buttons objectAtIndex:index];
button.userInteractionEnabled = enabled;
button.alpha = enabled ? 1.0 : 0.25;
}
}
@end