-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathILSideScrollView.m
More file actions
161 lines (125 loc) · 4.61 KB
/
ILSideScrollView.m
File metadata and controls
161 lines (125 loc) · 4.61 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
//
// ILSideScrollView.m
// Version 1.0
// Created by Isaac Lim (isaacl.net) on 1/1/13.
//
#import "ILSideScrollView.h"
/* Default Settings */
#define kILSideScrollViewBackgroundColor [UIColor whiteColor]
#define kILSideScrollViewIndicatorStyle UIScrollViewIndicatorStyleDefault
#define kILSideScrollViewItemBorderColor [UIColor blackColor]
/* Buffer distances */
#define kVerticalBuffer 10.0f
#define kHorizontalBuffer kVerticalBuffer
@interface ILSideScrollView() {
CGFloat leftLength;
CGFloat width;
UIColor *_borderColor;
}
@end
@implementation ILSideScrollView
@synthesize items = _items;
#pragma mark - Init methods
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupDefaults];
}
return self;
}
- (void)setupDefaults {
self.scrollsToTop = NO;
leftLength = kHorizontalBuffer;
/* Default settings */
[self setBackgroundColor:kILSideScrollViewBackgroundColor
indicatorStyle:kILSideScrollViewIndicatorStyle
itemBorderColor:kILSideScrollViewItemBorderColor];
}
- (void)setBackgroundColor:(UIColor *)backgroundColor
indicatorStyle:(UIScrollViewIndicatorStyle)style
itemBorderColor:(UIColor *)borderColor
{
self.backgroundColor = backgroundColor;
self.indicatorStyle = style;
_borderColor = borderColor;
}
#pragma mark - Data handling methods
- (void)populateSideScrollViewWithItems:(NSArray *)items {
[self removeAllItems];
self.items = [items mutableCopy];
width = self.frame.size.height - (2 * kVerticalBuffer);
/* Raise an exception if items contains foreign-typed objects */
if (![self arrayContainsItemObjects:items]) {
[NSException raise:@"ILSideScrollView array of wrong type" format:@"Array passed into populateSideScrollViewWithItems: contains objects that are not of type ILSideScrollViewItems."];
}
for (int i = 0; i < items.count; i++) {
ILSideScrollViewItem *item = items[i];
/* Create each square button */
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(leftLength, kVerticalBuffer, width, width);
btn.layer.borderWidth = 1.0f;
btn.layer.borderColor = _borderColor.CGColor;
/* Set the item's action */
btn.tag = i;
[btn addTarget:self
action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
/* Set the item's background properties */
[btn setBackgroundColor:item.backgroundColor];
[btn setBackgroundImage:item.defaultBackgroundImage
forState:UIControlStateNormal];
[btn setBackgroundImage:item.selectedBackgroundImage
forState:UIControlStateHighlighted];
/* Set the item's title properties */
[btn setTitle:item.title
forState:UIControlStateNormal];
[btn setTitleColor:item.defaultTitleColor
forState:UIControlStateNormal];
[btn setTitleColor:item.selectedTitleColor
forState:UIControlStateHighlighted];
btn.titleLabel.font = item.titleFont;
[self addSubview:btn];
leftLength += width + kHorizontalBuffer;
}
self.contentSize = CGSizeMake(leftLength, self.frame.size.height);
}
- (void)buttonTapped:(id)sender {
UIButton *btn = (UIButton *)sender;
NSUInteger idx = btn.tag;
ILSideScrollViewItem *item = self.items[idx];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
if (item.target != nil && item.action != nil) {
[item.target performSelector:item.action withObject:item.object];
}
#pragma clang diagnostic pop
}
/* Returns YES if array contains only ILSideScrollViewItem objects,
* NO otherwise.
*/
- (BOOL)arrayContainsItemObjects:(NSArray *)array {
for (int i = 0; i < array.count; i++) {
if (![array[i] isMemberOfClass:[ILSideScrollViewItem class]])
return NO;
}
return YES;
}
- (void)removeAllItems {
[self.items removeAllObjects];
leftLength = kHorizontalBuffer;
for (UIView *view in self.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
[view removeFromSuperview];
}
}
}
#pragma mark - Scrolling methods
- (void)scrollToLeftmost {
CGRect left = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self scrollRectToVisible:left animated:YES];
}
- (void)scrollToItemAtIndex:(NSUInteger)index {
CGFloat x = (kHorizontalBuffer+width) * (index+1);
[self scrollRectToVisible:CGRectMake(x, 1, 2*width, 1) animated:YES];
}
@end