-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmotionView.m
164 lines (129 loc) · 4.96 KB
/
EmotionView.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
//
// EmotionView.m
// My微博
//
// Created by Macx on 16/1/22.
// Copyright © 2016年 jinquanbin. All rights reserved.
//
#import "EmotionView.h"
#define kRowEmotions 4
#define kColumnEmotions 7
#define kRowSpace 5
#define kColumnSpace 5
#define kPageEmotions (kRowEmotions * kColumnEmotions)
#define kEmotionSize ((kScreenWidth - 8 * kColumnSpace) / kColumnEmotions)
@implementation EmotionView
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self createMagnifierView];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createMagnifierView];
}
return self;
}
//返回表情数组
- (NSArray *)getEmotionPlist {
NSString *path = [[NSBundle mainBundle] pathForResource:@"emotions" ofType:@"plist"];
NSArray *emotions = [NSArray arrayWithContentsOfFile:path];
return emotions;
}
- (void)drawRect:(CGRect)rect {
NSArray *emotions = [self getEmotionPlist];
//绘制表情
//页数
NSInteger pageCount = emotions.count / kPageEmotions;
if (pageCount % kPageEmotions != 0) {
pageCount++;
}
//行列
int row = 0, column = 0, count = 0;
for (int i = 0; i < pageCount; i++) {
for (int j = 0; j < kPageEmotions; j++) {
count++;
row = j / kColumnEmotions;
column = j % kColumnEmotions;
UIImage *image = [UIImage imageNamed:emotions[i*kPageEmotions + j][@"png"]];
[image drawInRect:CGRectMake(i*kScreenWidth + column*(kEmotionSize+kColumnSpace) + kColumnSpace, kRowSpace + row*(kEmotionSize+kRowSpace), kEmotionSize, kEmotionSize)];
if (count == emotions.count) {
break;
}
}
}
}
- (void)createMagnifierView {
UIImageView *magnifierView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"emoticon_keyboard_magnifier"]];
magnifierView.frame = CGRectMake(0, 0, magnifierView.width, magnifierView.height);
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake((magnifierView.image.size.width - 40) / 2, 12, 40, 40)];
img.tag = 100;
[magnifierView addSubview:img];
magnifierView.hidden = YES;
[self addSubview:magnifierView];
}
#pragma mark - 触摸事件处理
- (void)touchEmotion:(CGPoint)point {
CGFloat x = point.x;
CGFloat y = point.y;
NSInteger selectPage = x / kScreenWidth;
NSInteger selectRow = y / (kEmotionSize+kRowSpace);
NSInteger selectColumn = (x - selectPage*kScreenWidth) / (kEmotionSize+kColumnSpace);
NSArray *emotions = [self getEmotionPlist];
UIImageView *selectImg = [self viewWithTag:100];
selectImg.image = [UIImage imageNamed:emotions[selectPage*kPageEmotions + selectRow*kColumnEmotions + selectColumn][@"png"]];
selectImg.superview.hidden = NO;
UIImageView *magnifier = (UIImageView *)[selectImg superview];
magnifier.frame = CGRectMake(selectColumn*(kEmotionSize+kColumnSpace), (selectRow-1)*(kEmotionSize+kRowSpace), magnifier.width, magnifier.height);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
((UIScrollView *)self.superview).scrollEnabled = NO;
[self touchEmotion:point];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UIImageView *selectImg = [self viewWithTag:100];
selectImg.superview.hidden = YES;
((UIScrollView *)self.superview).scrollEnabled = YES;
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGFloat x = point.x;
CGFloat y = point.y;
//确定表情位置
NSInteger selectPage = x / kScreenWidth;
NSInteger selectRow = y / (kEmotionSize+kRowSpace);
NSInteger selectColumn = (x - selectPage*kScreenWidth) / (kEmotionSize+kColumnSpace);
selectRow = MAX(0, MIN(kRowEmotions, selectRow));
selectColumn = MAX(0, MIN(kColumnEmotions, selectColumn));
NSArray *emotions = [self getEmotionPlist];
NSInteger selectInteger = selectPage*kPageEmotions + selectRow*kColumnEmotions + selectColumn;
//异常处理
if (selectInteger >= emotions.count) {
return;
}
//block 回调
NSString *emotionName = emotions[selectInteger][@"chs"];
//安全性检查
if (!emotionName || [emotionName isKindOfClass:[NSNull class]]) {
return;
}
if (_emotionBlock) {
_emotionBlock(emotions[selectInteger][@"chs"]);
}
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UIImageView *selectImg = [self viewWithTag:100];
selectImg.superview.hidden = YES;
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
[self touchEmotion:point];
}
@end