-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathFLEXExplorerToolbar.m
More file actions
256 lines (202 loc) · 10.5 KB
/
FLEXExplorerToolbar.m
File metadata and controls
256 lines (202 loc) · 10.5 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//
// FLEXExplorerToolbar.m
// Flipboard
//
// Created by Ryan Olson on 4/4/14.
// Copyright (c) 2020 FLEX Team. All rights reserved.
//
#import "FLEXColor.h"
#import "FLEXExplorerToolbar.h"
#import "FLEXExplorerToolbarItem.h"
#import "FLEXResources.h"
#import "FLEXUtility.h"
@interface FLEXExplorerToolbar ()
@property (nonatomic, readwrite) FLEXExplorerToolbarItem *globalsItem;
@property (nonatomic, readwrite) FLEXExplorerToolbarItem *hierarchyItem;
@property (nonatomic, readwrite) FLEXExplorerToolbarItem *selectItem;
@property (nonatomic, readwrite) FLEXExplorerToolbarItem *recentItem;
@property (nonatomic, readwrite) FLEXExplorerToolbarItem *moveItem;
@property (nonatomic, readwrite) FLEXExplorerToolbarItem *closeItem;
@property (nonatomic, readwrite) UIView *dragHandle;
@property (nonatomic) UIImageView *dragHandleImageView;
@property (nonatomic) UIView *selectedViewDescriptionContainer;
@property (nonatomic) UIView *selectedViewDescriptionSafeAreaContainer;
@property (nonatomic) UIView *selectedViewColorIndicator;
@property (nonatomic) UILabel *selectedViewDescriptionLabel;
@property (nonatomic,readwrite) UIView *backgroundView;
@end
@implementation FLEXExplorerToolbar
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Background
self.backgroundView = [UIView new];
self.backgroundView.backgroundColor = [FLEXColor secondaryBackgroundColorWithAlpha:0.95];
[self addSubview:self.backgroundView];
// Drag handle
self.dragHandle = [UIView new];
self.dragHandle.backgroundColor = UIColor.clearColor;
self.dragHandleImageView = [[UIImageView alloc] initWithImage:FLEXResources.dragHandle];
self.dragHandleImageView.tintColor = [FLEXColor.iconColor colorWithAlphaComponent:0.666];
[self.dragHandle addSubview:self.dragHandleImageView];
[self addSubview:self.dragHandle];
// Buttons
self.globalsItem = [FLEXExplorerToolbarItem itemWithTitle:@"菜单" image:FLEXResources.globalsIcon];
self.hierarchyItem = [FLEXExplorerToolbarItem itemWithTitle:@"视图" image:FLEXResources.hierarchyIcon];
self.selectItem = [FLEXExplorerToolbarItem itemWithTitle:@"选择" image:FLEXResources.selectIcon];
self.recentItem = [FLEXExplorerToolbarItem itemWithTitle:@"最近" image:FLEXResources.recentIcon];
self.moveItem = [FLEXExplorerToolbarItem itemWithTitle:@"移动" image:FLEXResources.moveIcon sibling:self.recentItem];
self.closeItem = [FLEXExplorerToolbarItem itemWithTitle:@"关闭" image:FLEXResources.closeIcon];
// Selected view box //
self.selectedViewDescriptionContainer = [UIView new];
self.selectedViewDescriptionContainer.backgroundColor = [FLEXColor tertiaryBackgroundColorWithAlpha:0.95];
self.selectedViewDescriptionContainer.hidden = YES;
[self addSubview:self.selectedViewDescriptionContainer];
self.selectedViewDescriptionSafeAreaContainer = [UIView new];
self.selectedViewDescriptionSafeAreaContainer.backgroundColor = UIColor.clearColor;
[self.selectedViewDescriptionContainer addSubview:self.selectedViewDescriptionSafeAreaContainer];
self.selectedViewColorIndicator = [UIView new];
self.selectedViewColorIndicator.backgroundColor = UIColor.redColor;
[self.selectedViewDescriptionSafeAreaContainer addSubview:self.selectedViewColorIndicator];
self.selectedViewDescriptionLabel = [UILabel new];
self.selectedViewDescriptionLabel.backgroundColor = UIColor.clearColor;
self.selectedViewDescriptionLabel.font = [[self class] descriptionLabelFont];
[self.selectedViewDescriptionSafeAreaContainer addSubview:self.selectedViewDescriptionLabel];
// toolbarItems
self.toolbarItems = @[_globalsItem, _hierarchyItem, _selectItem, _moveItem, _closeItem];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect safeArea = [self safeArea];
// Drag Handle
const CGFloat kToolbarItemHeight = [[self class] toolbarItemHeight];
self.dragHandle.frame = CGRectMake(CGRectGetMinX(safeArea), CGRectGetMinY(safeArea), [[self class] dragHandleWidth], kToolbarItemHeight);
CGRect dragHandleImageFrame = self.dragHandleImageView.frame;
dragHandleImageFrame.origin.x = FLEXFloor((self.dragHandle.frame.size.width - dragHandleImageFrame.size.width) / 2.0);
dragHandleImageFrame.origin.y = FLEXFloor((self.dragHandle.frame.size.height - dragHandleImageFrame.size.height) / 2.0);
self.dragHandleImageView.frame = dragHandleImageFrame;
// Toolbar Items
CGFloat originX = CGRectGetMaxX(self.dragHandle.frame);
CGFloat originY = CGRectGetMinY(safeArea);
CGFloat height = kToolbarItemHeight;
CGFloat width = FLEXFloor((CGRectGetWidth(safeArea) - CGRectGetWidth(self.dragHandle.frame)) / self.toolbarItems.count);
for (FLEXExplorerToolbarItem *toolbarItem in self.toolbarItems) {
toolbarItem.currentItem.frame = CGRectMake(originX, originY, width, height);
originX = CGRectGetMaxX(toolbarItem.currentItem.frame);
}
// Make sure the last toolbar item goes to the edge to account for any accumulated rounding effects.
UIView *lastToolbarItem = self.toolbarItems.lastObject.currentItem;
CGRect lastToolbarItemFrame = lastToolbarItem.frame;
lastToolbarItemFrame.size.width = CGRectGetMaxX(safeArea) - lastToolbarItemFrame.origin.x;
lastToolbarItem.frame = lastToolbarItemFrame;
self.backgroundView.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), kToolbarItemHeight);
const CGFloat kSelectedViewColorDiameter = [[self class] selectedViewColorIndicatorDiameter];
const CGFloat kDescriptionLabelHeight = [[self class] descriptionLabelHeight];
const CGFloat kHorizontalPadding = [[self class] horizontalPadding];
const CGFloat kDescriptionVerticalPadding = [[self class] descriptionVerticalPadding];
const CGFloat kDescriptionContainerHeight = [[self class] descriptionContainerHeight];
CGRect descriptionContainerFrame = CGRectZero;
descriptionContainerFrame.size.width = CGRectGetWidth(self.bounds);
descriptionContainerFrame.size.height = kDescriptionContainerHeight;
descriptionContainerFrame.origin.x = CGRectGetMinX(self.bounds);
descriptionContainerFrame.origin.y = CGRectGetMaxY(self.bounds) - kDescriptionContainerHeight;
self.selectedViewDescriptionContainer.frame = descriptionContainerFrame;
CGRect descriptionSafeAreaContainerFrame = CGRectZero;
descriptionSafeAreaContainerFrame.size.width = CGRectGetWidth(safeArea);
descriptionSafeAreaContainerFrame.size.height = kDescriptionContainerHeight;
descriptionSafeAreaContainerFrame.origin.x = CGRectGetMinX(safeArea);
descriptionSafeAreaContainerFrame.origin.y = CGRectGetMinY(safeArea);
self.selectedViewDescriptionSafeAreaContainer.frame = descriptionSafeAreaContainerFrame;
// Selected View Color
CGRect selectedViewColorFrame = CGRectZero;
selectedViewColorFrame.size.width = kSelectedViewColorDiameter;
selectedViewColorFrame.size.height = kSelectedViewColorDiameter;
selectedViewColorFrame.origin.x = kHorizontalPadding;
selectedViewColorFrame.origin.y = FLEXFloor((kDescriptionContainerHeight - kSelectedViewColorDiameter) / 2.0);
self.selectedViewColorIndicator.frame = selectedViewColorFrame;
self.selectedViewColorIndicator.layer.cornerRadius = ceil(selectedViewColorFrame.size.height / 2.0);
// Selected View Description
CGRect descriptionLabelFrame = CGRectZero;
CGFloat descriptionOriginX = CGRectGetMaxX(selectedViewColorFrame) + kHorizontalPadding;
descriptionLabelFrame.size.height = kDescriptionLabelHeight;
descriptionLabelFrame.origin.x = descriptionOriginX;
descriptionLabelFrame.origin.y = kDescriptionVerticalPadding;
descriptionLabelFrame.size.width = CGRectGetMaxX(self.selectedViewDescriptionContainer.bounds) - kHorizontalPadding - descriptionOriginX;
self.selectedViewDescriptionLabel.frame = descriptionLabelFrame;
}
#pragma mark - Setter Overrides
- (void)setToolbarItems:(NSArray<FLEXExplorerToolbarItem *> *)toolbarItems {
if (_toolbarItems == toolbarItems) {
return;
}
// Remove old toolbar items, if any
for (FLEXExplorerToolbarItem *item in _toolbarItems) {
[item.currentItem removeFromSuperview];
}
// Trim to 5 items if necessary
if (toolbarItems.count > 5) {
toolbarItems = [toolbarItems subarrayWithRange:NSMakeRange(0, 5)];
}
for (FLEXExplorerToolbarItem *item in toolbarItems) {
[self addSubview:item.currentItem];
}
_toolbarItems = toolbarItems.copy;
// Lay out new items
[self setNeedsLayout];
[self layoutIfNeeded];
}
- (void)setSelectedViewOverlayColor:(UIColor *)selectedViewOverlayColor {
if (![_selectedViewOverlayColor isEqual:selectedViewOverlayColor]) {
_selectedViewOverlayColor = selectedViewOverlayColor;
self.selectedViewColorIndicator.backgroundColor = selectedViewOverlayColor;
}
}
- (void)setSelectedViewDescription:(NSString *)selectedViewDescription {
if (![_selectedViewDescription isEqual:selectedViewDescription]) {
_selectedViewDescription = selectedViewDescription;
self.selectedViewDescriptionLabel.text = selectedViewDescription;
BOOL showDescription = selectedViewDescription.length > 0;
self.selectedViewDescriptionContainer.hidden = !showDescription;
}
}
#pragma mark - Sizing Convenience Methods
+ (UIFont *)descriptionLabelFont {
return [UIFont systemFontOfSize:12.0];
}
+ (CGFloat)toolbarItemHeight {
return 44.0;
}
+ (CGFloat)dragHandleWidth {
return FLEXResources.dragHandle.size.width;
}
+ (CGFloat)descriptionLabelHeight {
return ceil([[self descriptionLabelFont] lineHeight]);
}
+ (CGFloat)descriptionVerticalPadding {
return 2.0;
}
+ (CGFloat)descriptionContainerHeight {
return [self descriptionVerticalPadding] * 2.0 + [self descriptionLabelHeight];
}
+ (CGFloat)selectedViewColorIndicatorDiameter {
return ceil([self descriptionLabelHeight] / 2.0);
}
+ (CGFloat)horizontalPadding {
return 11.0;
}
- (CGSize)sizeThatFits:(CGSize)size {
CGFloat height = 0.0;
height += [[self class] toolbarItemHeight];
height += [[self class] descriptionContainerHeight];
return CGSizeMake(size.width, height);
}
- (CGRect)safeArea {
CGRect safeArea = self.bounds;
if (@available(iOS 11.0, *)) {
safeArea = UIEdgeInsetsInsetRect(self.bounds, self.safeAreaInsets);
}
return safeArea;
}
@end