forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEaseToolBar.m
More file actions
executable file
·121 lines (108 loc) · 4.46 KB
/
EaseToolBar.m
File metadata and controls
executable file
·121 lines (108 loc) · 4.46 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
//
// EaseToolBar.m
// Coding_iOS
//
// Created by Ease on 14/11/27.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#define kEaseToolBar_Height 49.0
#define kEaseToolBar_SplitLineViewTag 100
#import "EaseToolBar.h"
@interface EaseToolBar ()
@property (strong, nonatomic) NSArray *buttonItems;
@end
@implementation EaseToolBar
+ (instancetype)easeToolBarWithItems:(NSArray *)buttonItems{
return [[EaseToolBar alloc] initWithItems:buttonItems];
}
- (id)itemOfIndex:(NSInteger)index{
if (index >= 0 && self.buttonItems && self.buttonItems.count >= index) {
return self.buttonItems[index];
}else{
return nil;
}
}
- (instancetype)initWithItems:(NSArray *)buttonItems{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, kScreen_Width, kEaseToolBar_Height);
self.backgroundColor = [UIColor colorWithHexString:@"0xf8f8f8"];
self.buttonItems = buttonItems;
}
return self;
}
- (void)setButtonItems:(NSArray *)buttonItems{
if (buttonItems != _buttonItems) {
for (UIView *view in self.subviews) {
[view removeFromSuperview];
}
_buttonItems = buttonItems;
}
[self addLineUp:YES andDown:NO andColor:[UIColor lightGrayColor]];
if (_buttonItems.count > 0) {
NSInteger num = _buttonItems.count;
CGFloat itemWidth = CGRectGetWidth(self.frame)/num;
CGFloat itemHeight = CGRectGetHeight(self.frame);
for (int i = 0; i < num; i++) {
UIControl *item = _buttonItems[i];
item.frame = CGRectMake(i*itemWidth, 0, itemWidth, itemHeight);
[item addTarget:self action:@selector(itemButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:item];
if (i < num-1) {//item之间的分隔线
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake((i+1)*itemWidth, (itemHeight - 25.0)/2, 0.5, 25)];
lineView.tag = kEaseToolBar_SplitLineViewTag;
lineView.backgroundColor = [UIColor colorWithHexString:@"0xd1d1d1"];
[self addSubview:lineView];
}
}
}
}
- (void)itemButtonClicked:(UIButton *)sender{
NSInteger index = [self.buttonItems indexOfObject:sender];
if (index != NSNotFound && [self.delegate respondsToSelector:@selector(easeToolBar:didClickedIndex:)]) {
[self.delegate easeToolBar:self didClickedIndex:index];
}
}
@end
@interface EaseToolBarItem ()
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *imageName;
@property (strong, nonatomic) NSString *disableImageName;
@end
@implementation EaseToolBarItem
+ (instancetype)easeToolBarItemWithTitle:(NSString *)title image:(NSString *)imageName disableImage:(NSString *)disableImageName{
return [[EaseToolBarItem alloc] initWithTitle:title image:imageName disableImage:disableImageName];
}
- (instancetype)initWithTitle:(NSString *)title image:(NSString *)imageName disableImage:(NSString *)disableImageName{
self = [super init];
if (self) {
self.title = title;
self.imageName = imageName;
self.disableImageName = disableImageName;
[self setIconImage:[UIImage imageNamed:_imageName]];
[self setButtonText:_title];
[self setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],
NSForegroundColorAttributeName:[UIColor colorWithHexString:@"0x888888"]} forUIControlState:UIControlStateNormal];
[self setIconPosition:IconPositionLeft];
[self setTextAlignment:NSTextAlignmentCenter];
[self setControlState:UIControlStateNormal];
self.enabled = YES;
}
return self;
}
- (void)setEnabled:(BOOL)enabled{
[super setEnabled:enabled];
NSString *imageName = enabled? _imageName:(_disableImageName? _disableImageName: _imageName);
NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],
NSForegroundColorAttributeName:enabled? [UIColor colorWithHexString:@"0x888888"] : [UIColor colorWithHexString:@"0xc2c2c2"]};
[self setIconImage:[UIImage imageNamed:imageName]];
[self setAttributes:attributes forUIControlState:UIControlStateNormal];
}
- (void)addTipIcon{
CGRect iconFrame = [self getIconImageView].frame;
[self addBadgeTip:kBadgeTipStr withCenterPosition:CGPointMake(iconFrame.origin.x + iconFrame.size.width +2, 12)];
}
- (void)removeTipIcon{
[self removeBadgeTips];
}
@end