单军华
2018-07-11 acdf41fa3b32b628d9d7bba1f975060567dad3d7
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
//
//  HMEmoticonToolbar.m
//  表情键盘
//
//  Created by 刘凡 on 16/3/3.
//  Copyright © 2016年 itcast. All rights reserved.
//
 
#import "HMEmoticonToolbar.h"
#import "UIImage+HMEmoticon.h"
#import "HMEmoticonManager.h"
 
/// 按钮 tag 起始数值
static NSInteger kEmoticonToolbarTagBaseValue = 1000;
 
@implementation HMEmoticonToolbar {
    UIButton *_selectedButton;
}
 
#pragma mark - 构造函数
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
    }
    return self;
}
 
- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self prepareUI];
    }
    return self;
}
 
- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 计算分组按钮位置
    CGRect rect = self.bounds;
    CGFloat w = rect.size.width / self.subviews.count;
    rect.size.width = w;
    
    int index = 0;
    for (UIView *v in self.subviews) {
        v.frame = CGRectOffset(rect, index++ * w, 0);
    }
}
 
#pragma mark - 公共方法
- (void)selectSection:(NSInteger)section {
    UIButton *button = [self viewWithTag:(section + kEmoticonToolbarTagBaseValue)];
    
    [self selectedButtonWithButton:button];
}
 
#pragma mark - 监听方法
/// 点击工具栏按钮
- (void)clickToolbarButton:(UIButton *)button {
    if (button == _selectedButton) {
        return;
    }
    [self selectedButtonWithButton:button];
    
    [self.delegate emoticonToolbarDidSelectSection:button.tag - kEmoticonToolbarTagBaseValue];
}
 
/// 将指定的按钮设置为选中按钮
- (void)selectedButtonWithButton:(UIButton *)button {
    button.selected = !button.selected;
    _selectedButton.selected = !_selectedButton.selected;
    _selectedButton = button;
}
 
#pragma mark - 设置界面
- (void)prepareUI {
    
    NSArray *packages = [HMEmoticonManager sharedManager].packages;
    
    // 创建按钮
    NSInteger index = 0;
    for (HMEmoticonPackage *package in packages) {
        [self addChildButton:package.groupName bgImageName:package.bgImageName type:index++];
    }
}
 
- (void)addChildButton:(NSString *)title bgImageName:(NSString *)bgImageName type:(NSInteger)type {
    UIButton *btn = [[UIButton alloc] init];
    
    btn.tag = type + kEmoticonToolbarTagBaseValue;
    
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
    
    NSString *imageName = [NSString stringWithFormat:@"compose_emotion_table_%@_normal", bgImageName];
    NSString *imageNameSL = [NSString stringWithFormat:@"compose_emotion_table_%@_selected", bgImageName];
    
    [btn setBackgroundImage:[[UIImage hm_imageNamed:imageName] hm_resizableImage] forState:UIControlStateNormal];
    [btn setBackgroundImage:[[UIImage hm_imageNamed:imageNameSL] hm_resizableImage] forState:UIControlStateSelected];
    
    [btn addTarget:self action:@selector(clickToolbarButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:btn];
}
 
@end