-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSJHorizontalMenu.m
327 lines (255 loc) · 15.8 KB
/
SJHorizontalMenu.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//
// SJHorizontalMenu.m
// WesCafe
//
// Created by Sandeep Jangir on 14/02/16.
// Copyright © 2016 Sandeep Jangir. All rights reserved.
//
#import "SJHorizontalMenu.h"
#import "SJHorizontalMenuScrollView.h"
#define kSJHorizontalSelectionListHorizontalMargin 10
#define kSJHorizontalSelectionListInternalPadding 15
#define kSJHorizontalSelectionListSelectionIndicatorHeight 3
#define kSJHorizontalSelectionListTrimHeight 0.5
@implementation SJHorizontalMenu
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
_scrollView = [[SJHorizontalMenuScrollView alloc] init];
_scrollView.backgroundColor = [UIColor clearColor];
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.scrollsToTop = NO;
_scrollView.canCancelContentTouches = YES;
_scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_scrollView];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_scrollView]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(_scrollView)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(_scrollView)]];
_contentView = [[UIView alloc] init];
_contentView.translatesAutoresizingMaskIntoConstraints = NO;
[_scrollView addSubview:_contentView];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_contentView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:_contentView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_contentView]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(_contentView)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_contentView]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(_contentView)]];
_bottomTrim = [[UIView alloc] init];
_bottomTrim.backgroundColor = [UIColor blackColor];
_bottomTrim.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_bottomTrim];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_bottomTrim]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(_bottomTrim)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_bottomTrim(height)]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:@{@"height" : @(kSJHorizontalSelectionListTrimHeight)}
views:NSDictionaryOfVariableBindings(_bottomTrim)]];
self.buttonInsets = UIEdgeInsetsMake(5, 5, 5, 5);
self.selectionIndicatorStyle = SJHorizontalSelectionIndicatorStyleBottomBar;
_buttons = [NSMutableArray array];
_selectionIndicatorBar = [[UIView alloc] init];
_selectionIndicatorBar.translatesAutoresizingMaskIntoConstraints = NO;
_selectionIndicatorBar.backgroundColor = [UIColor blackColor];
_buttonColorsByState = [NSMutableDictionary dictionary];
_buttonColorsByState[@(UIControlStateNormal)] = [UIColor blackColor];
}
return self;
}
#pragma mark - Custom Getters and Setters
- (void)setSelectionIndicatorColor:(UIColor *)selectionIndicatorColor {
self.selectionIndicatorBar.backgroundColor = selectionIndicatorColor;
if (!self.buttonColorsByState[@(UIControlStateSelected)]) {
self.buttonColorsByState[@(UIControlStateSelected)] = selectionIndicatorColor;
}
}
- (UIColor *)selectionIndicatorColor {
return self.selectionIndicatorBar.backgroundColor;
}
- (void)setBottomTrimColor:(UIColor *)bottomTrimColor {
self.bottomTrim.backgroundColor = bottomTrimColor;
}
- (UIColor *)bottomTrimColor {
return self.bottomTrim.backgroundColor;
}
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state {
self.buttonColorsByState[@(state)] = color;
}
#pragma mark - Public Methods
- (void)reloadData {
for (UIButton *button in self.buttons) {
[button removeFromSuperview];
}
[self.selectionIndicatorBar removeFromSuperview];
[self.buttons removeAllObjects];
NSInteger totalButtons = [self.dataSource numberOfItemsInSelectionMenu:self];
if (totalButtons < 1) {
return;
}
UIButton *previousButton;
for (NSInteger index = 0; index < totalButtons; index++) {
NSString *buttonTitle = [self.dataSource selectionList:self titleForItemWithIndex:index];
UIButton *button = [self selectionListButtonWithTitle:buttonTitle];
[self.contentView addSubview:button];
if (previousButton) {
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[previousButton]-padding-[button]"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:@{@"padding" : @(kSJHorizontalSelectionListInternalPadding)}
views:NSDictionaryOfVariableBindings(previousButton, button)]];
} else {
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-margin-[button]"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:@{@"margin" : @(kSJHorizontalSelectionListHorizontalMargin)}
views:NSDictionaryOfVariableBindings(button)]];
}
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
previousButton = button;
[self.buttons addObject:button];
}
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[previousButton]-margin-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:@{@"margin" : @(kSJHorizontalSelectionListHorizontalMargin)}
views:NSDictionaryOfVariableBindings(previousButton)]];
if (totalButtons > 0) {
UIButton *selectedButton = self.buttons[self.selectedButtonIndex];
selectedButton.selected = YES;
switch (self.selectionIndicatorStyle) {
case SJHorizontalSelectionIndicatorStyleBottomBar: {
[self.contentView addSubview:self.selectionIndicatorBar];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_selectionIndicatorBar(height)]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:@{@"height" : @(kSJHorizontalSelectionListSelectionIndicatorHeight)}
views:NSDictionaryOfVariableBindings(_selectionIndicatorBar)]];
[self alignSelectionIndicatorWithButton:selectedButton];
break;
}
case SJHorizontalSelectionIndicatorStyleButtonBorder: {
selectedButton.layer.borderColor = self.selectionIndicatorColor.CGColor;
break;
}
}
}
[self sendSubviewToBack:self.bottomTrim];
[self updateConstraintsIfNeeded];
}
- (void)layoutSubviews {
if (!self.buttons.count) {
[self reloadData];
}
[super layoutSubviews];
}
#pragma mark - Private Methods
- (UIButton *)selectionListButtonWithTitle:(NSString *)buttonTitle {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.contentEdgeInsets = self.buttonInsets;
[button setTitle:buttonTitle forState:UIControlStateNormal];
for (NSNumber *controlState in [self.buttonColorsByState allKeys]) {
[button setTitleColor:self.buttonColorsByState[controlState] forState:controlState.integerValue];
}
button.titleLabel.font = [UIFont systemFontOfSize:13];
[button sizeToFit];
if (self.selectionIndicatorStyle == SJHorizontalSelectionIndicatorStyleButtonBorder) {
button.layer.borderWidth = 1.0;
button.layer.cornerRadius = 3.0;
button.layer.borderColor = [UIColor clearColor].CGColor;
button.layer.masksToBounds = YES;
}
[button addTarget:self
action:@selector(buttonWasTapped:)
forControlEvents:UIControlEventTouchUpInside];
button.translatesAutoresizingMaskIntoConstraints = NO;
return button;
}
- (void)setupSelectedButton:(UIButton *)selectedButton oldSelectedButton:(UIButton *)oldSelectedButton {
switch (self.selectionIndicatorStyle) {
case SJHorizontalSelectionIndicatorStyleBottomBar: {
[self.contentView removeConstraint:self.leftSelectionIndicatorConstraint];
[self.contentView removeConstraint:self.rightSelectionIndicatorConstraint];
[self alignSelectionIndicatorWithButton:selectedButton];
[self layoutIfNeeded];
break;
}
case SJHorizontalSelectionIndicatorStyleButtonBorder: {
selectedButton.layer.borderColor = self.selectionIndicatorColor.CGColor;
oldSelectedButton.layer.borderColor = [UIColor clearColor].CGColor;
break;
}
}
}
- (void)alignSelectionIndicatorWithButton:(UIButton *)button {
self.leftSelectionIndicatorConstraint = [NSLayoutConstraint constraintWithItem:self.selectionIndicatorBar
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:button
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0.0];
[self.contentView addConstraint:self.leftSelectionIndicatorConstraint];
self.rightSelectionIndicatorConstraint = [NSLayoutConstraint constraintWithItem:self.selectionIndicatorBar
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:button
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0.0];
[self.contentView addConstraint:self.rightSelectionIndicatorConstraint];
}
#pragma mark - Action Handlers
- (void)buttonWasTapped:(id)sender {
NSInteger index = [self.buttons indexOfObject:sender];
if (index != NSNotFound) {
if (index == self.selectedButtonIndex) {
return;
}
UIButton *oldSelectedButton = self.buttons[self.selectedButtonIndex];
oldSelectedButton.selected = NO;
self.selectedButtonIndex = index;
UIButton *tappedButton = (UIButton *)sender;
tappedButton.selected = YES;
[self layoutIfNeeded];
[UIView animateWithDuration:0.4
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0
options:UIViewAnimationOptionCurveLinear
animations:^{
[self setupSelectedButton:tappedButton oldSelectedButton:oldSelectedButton];
}
completion:nil];
[self.scrollView scrollRectToVisible:CGRectInset(tappedButton.frame, -kSJHorizontalSelectionListHorizontalMargin, 0)
animated:YES];
if ([self.delegate respondsToSelector:@selector(selectionList:didSelectButtonWithIndex:)]) {
[self.delegate selectionList:self didSelectButtonWithIndex:index];
}
}
}
@end