-
Notifications
You must be signed in to change notification settings - Fork 8
/
YSLScrollMenuView.m
executable file
·197 lines (165 loc) · 6.83 KB
/
YSLScrollMenuView.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
//
// YSLScrollMenuView.m
// YSLContainerViewController
//
// Created by yamaguchi on 2015/03/03.
// Copyright (c) 2015年 h.yamaguchi. All rights reserved.
//
#import "YSLScrollMenuView.h"
static const CGFloat kYSLScrollMenuViewWidth = 80;
static const CGFloat kYSLScrollMenuViewMargin = 5;
static const CGFloat kYSLIndicatorHeight = 4;
@interface YSLScrollMenuView ()
@property (nonatomic, strong) UIView *indicatorView;
@end
@implementation YSLScrollMenuView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// default
_viewbackgroudColor = [UIColor whiteColor];
_itemfont = [UIFont systemFontOfSize:16];
_itemTitleColor = [UIColor colorWithRed:0.866667 green:0.866667 blue:0.866667 alpha:1.0];
_itemSelectedTitleColor = [UIColor colorWithRed:0.333333 green:0.333333 blue:0.333333 alpha:1.0];
_itemIndicatorColor = [UIColor colorWithRed:0.168627 green:0.498039 blue:0.839216 alpha:1.0];
self.backgroundColor = _viewbackgroudColor;
_scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
_scrollView.showsHorizontalScrollIndicator = NO;
[self addSubview:_scrollView];
}
return self;
}
#pragma mark -- Setter
- (void)setViewbackgroudColor:(UIColor *)viewbackgroudColor
{
if (!viewbackgroudColor) { return; }
_viewbackgroudColor = viewbackgroudColor;
self.backgroundColor = viewbackgroudColor;
}
- (void)setItemfont:(UIFont *)itemfont
{
if (!itemfont) { return; }
_itemfont = itemfont;
for (UILabel *label in _itemTitleArray) {
label.font = itemfont;
}
}
- (void)setItemTitleColor:(UIColor *)itemTitleColor
{
if (!itemTitleColor) { return; }
_itemTitleColor = itemTitleColor;
for (UILabel *label in _itemTitleArray) {
label.textColor = itemTitleColor;
}
}
- (void)setItemIndicatorColor:(UIColor *)itemIndicatorColor
{
if (!itemIndicatorColor) { return; }
_itemIndicatorColor = itemIndicatorColor;
_indicatorView.backgroundColor = itemIndicatorColor;
}
- (void)setItemTitleArray:(NSArray *)itemTitleArray
{
if (_itemTitleArray != itemTitleArray) {
_itemTitleArray = itemTitleArray;
NSMutableArray *views = [NSMutableArray array];
for (int i = 0; i < itemTitleArray.count; i++) {
CGRect frame = CGRectMake(0, 0, kYSLScrollMenuViewWidth, CGRectGetHeight(self.frame));
UILabel *itemView = [[UILabel alloc] initWithFrame:frame];
[self.scrollView addSubview:itemView];
itemView.tag = i;
itemView.text = itemTitleArray[i];
itemView.userInteractionEnabled = YES;
itemView.backgroundColor = [UIColor clearColor];
itemView.textAlignment = NSTextAlignmentCenter;
itemView.font = self.itemfont;
itemView.textColor = _itemTitleColor;
[views addObject:itemView];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(itemViewTapAction:)];
[itemView addGestureRecognizer:tapGesture];
}
self.itemViewArray = [NSArray arrayWithArray:views];
// indicator
_indicatorView = [[UIView alloc]init];
_indicatorView.frame = CGRectMake(10, _scrollView.frame.size.height - kYSLIndicatorHeight, kYSLScrollMenuViewWidth, kYSLIndicatorHeight);
_indicatorView.backgroundColor = self.itemIndicatorColor;
[_scrollView addSubview:_indicatorView];
}
}
#pragma mark -- public
- (void)setIndicatorViewFrameWithRatio:(CGFloat)ratio isNextItem:(BOOL)isNextItem toIndex:(NSInteger)toIndex
{
CGFloat indicatorX = 0.0;
if (isNextItem) {
indicatorX = ((kYSLScrollMenuViewMargin + kYSLScrollMenuViewWidth) * ratio ) + (toIndex * kYSLScrollMenuViewWidth) + ((toIndex + 1) * kYSLScrollMenuViewMargin);
} else {
indicatorX = ((kYSLScrollMenuViewMargin + kYSLScrollMenuViewWidth) * (1 - ratio) ) + (toIndex * kYSLScrollMenuViewWidth) + ((toIndex + 1) * kYSLScrollMenuViewMargin);
}
if (indicatorX < kYSLScrollMenuViewMargin || indicatorX > self.scrollView.contentSize.width - (kYSLScrollMenuViewMargin + kYSLScrollMenuViewWidth)) {
return;
}
_indicatorView.frame = CGRectMake(indicatorX, _scrollView.frame.size.height - kYSLIndicatorHeight, kYSLScrollMenuViewWidth, kYSLIndicatorHeight);
// NSLog(@"retio : %f",_indicatorView.frame.origin.x);
}
- (void)setItemTextColor:(UIColor *)itemTextColor
seletedItemTextColor:(UIColor *)selectedItemTextColor
currentIndex:(NSInteger)currentIndex
{
if (itemTextColor) { _itemTitleColor = itemTextColor; }
if (selectedItemTextColor) { _itemSelectedTitleColor = selectedItemTextColor; }
for (int i = 0; i < self.itemViewArray.count; i++) {
UILabel *label = self.itemViewArray[i];
if (i == currentIndex) {
label.alpha = 0.0;
[UIView animateWithDuration:0.75
delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^{
label.alpha = 1.0;
label.textColor = _itemSelectedTitleColor;
} completion:^(BOOL finished) {
}];
} else {
label.textColor = _itemTitleColor;
}
}
}
#pragma mark -- private
// menu shadow
- (void)setShadowView
{
UIView *view = [[UIView alloc]init];
view.frame = CGRectMake(0, self.frame.size.height - 0.5, CGRectGetWidth(self.frame), 0.5);
view.backgroundColor = [UIColor lightGrayColor];
[self addSubview:view];
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat x = kYSLScrollMenuViewMargin;
for (NSUInteger i = 0; i < self.itemViewArray.count; i++) {
CGFloat width = kYSLScrollMenuViewWidth;
UIView *itemView = self.itemViewArray[i];
itemView.frame = CGRectMake(x, 0, width, self.scrollView.frame.size.height);
x += width + kYSLScrollMenuViewMargin;
}
self.scrollView.contentSize = CGSizeMake(x, self.scrollView.frame.size.height);
CGRect frame = self.scrollView.frame;
if (self.frame.size.width > x) {
frame.origin.x = (self.frame.size.width - x) / 2;
frame.size.width = x;
} else {
frame.origin.x = 0;
frame.size.width = self.frame.size.width;
}
self.scrollView.frame = frame;
}
#pragma mark -- Selector --------------------------------------- //
- (void)itemViewTapAction:(UITapGestureRecognizer *)Recongnizer
{
if (self.delegate && [self.delegate respondsToSelector:@selector(scrollMenuViewSelectedIndex:)]) {
[self.delegate scrollMenuViewSelectedIndex:[(UIGestureRecognizer*) Recongnizer view].tag];
}
}
@end