单军华
2018-07-12 3e8437ae559487362fae3525beb79c534c213a51
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
//
//  HMEmoticonCell.m
//  表情键盘
//
//  Created by 刘凡 on 16/3/3.
//  Copyright © 2016年 itcast. All rights reserved.
//
 
#import "HMEmoticonCell.h"
#import "HMEmoticon.h"
#import "UIImage+HMEmoticon.h"
#import "HMEmoticonTipView.h"
#import "HMEmoticonButton.h"
 
@implementation HMEmoticonCell {
    HMEmoticonTipView *_tipView;
    UILabel *_recentLabel;
    HMEmoticonButton *_deleteButton;
}
 
#pragma mark - 设置数据
- (void)setEmoticons:(NSArray<HMEmoticon *> *)emoticons {
    _emoticons = emoticons;
    
    for (UIView *v in self.contentView.subviews) {
        v.hidden = YES;
    }
    _deleteButton.hidden = NO;
    
    NSInteger index = 0;
    for (HMEmoticon *e in _emoticons) {
        HMEmoticonButton *btn = self.contentView.subviews[index++];
        
        btn.emoticon = e;
    }
}
 
- (void)setIndexPath:(NSIndexPath *)indexPath {
    _indexPath = indexPath;
    _recentLabel.hidden = indexPath.section != 0;
}
 
#pragma mark - 构造函数
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        
        // 添加手势监听
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]
                                                          initWithTarget:self
                                                          action:@selector(longPressed:)];
        
        longPressGesture.minimumPressDuration = 0.1;
        
        [self addGestureRecognizer:longPressGesture];
    }
    return self;
}
 
- (void)didMoveToWindow {
    [super didMoveToWindow];
    
    [self.window addSubview:_tipView];
    _tipView.hidden = YES;
}
 
#pragma mark - 监听方法
- (void)longPressed:(UILongPressGestureRecognizer *)recognizer {
    
    CGPoint location = [recognizer locationInView:self];
    
    // 查找选中按钮
    HMEmoticonButton *button = nil;
    for (HMEmoticonButton *btn in self.contentView.subviews) {
        if (CGRectContainsPoint(btn.frame, location) && !btn.hidden) {
            button = btn;
            break;
        }
    }
    if (button == nil) {
        _tipView.hidden = YES;
        
        return;
    }
 
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
        case UIGestureRecognizerStateChanged: {
            CGPoint buttonCenter = [self.contentView convertPoint:button.center toView:self.window];
            
            _tipView.center = buttonCenter;
            _tipView.emoticon = button.emoticon;
            
            _tipView.hidden = (button == _deleteButton);
        }
            break;
        case UIGestureRecognizerStateEnded:
            [self clickEmoticonButton:button];
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
            _tipView.hidden = YES;
            break;
        default:
            break;
    }
}
 
- (void)clickEmoticonButton:(HMEmoticonButton *)button {
    [self.delegate emoticonCellDidSelectedEmoticon:button.emoticon isRemoved:button.isDeleteButton];
}
 
#pragma mark - 设置界面
- (void)prepareUI {
    NSInteger rowCount = 3;
    NSInteger colCount = 7;
    
    CGFloat leftMargin = 8;
    CGFloat bottomMargin = 20;
    
    CGFloat w = ceil((self.bounds.size.width - 2 * leftMargin) / colCount);
    CGFloat h = ceil((self.bounds.size.height - bottomMargin) / rowCount);
    
    for (NSInteger i = 0; i < 21; i++) {
        NSInteger col = i % colCount;
        NSInteger row = i / colCount;
        
        CGRect rect = CGRectMake(col * w + leftMargin, row * h, w, h);
        UIButton *button = [HMEmoticonButton emoticonButtonWithFrame:rect tag:i];
        
        [self.contentView addSubview:button];
        
        [button addTarget:self
                   action:@selector(clickEmoticonButton:)
         forControlEvents:UIControlEventTouchUpInside];
    }
    
    // 删除按钮
    _deleteButton = ((HMEmoticonButton *)self.contentView.subviews.lastObject);
    _deleteButton.deleteButton = YES;
    
    // 提示视图
    _tipView = [[HMEmoticonTipView alloc] init];
    
    _recentLabel = [[UILabel alloc] init];
    _recentLabel.text = @"最近使用的表情";
    _recentLabel.textColor = [UIColor lightGrayColor];
    _recentLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:_recentLabel];
    
    _recentLabel.translatesAutoresizingMaskIntoConstraints = false;
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_recentLabel
                                                             attribute:NSLayoutAttributeCenterX
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.contentView
                                                             attribute:NSLayoutAttributeCenterX
                                                            multiplier:1
                                                              constant:0]];
    
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:_recentLabel
                                                             attribute:NSLayoutAttributeBottom
                                                             relatedBy:NSLayoutRelationEqual
                                                                toItem:self.contentView
                                                             attribute:NSLayoutAttributeBottom
                                                            multiplier:1
                                                              constant:-5]];
}
 
@end