这里获取到所用数据之后,然后再算每一页因该有多少个emojin,然后把每一页有多少个的,然后赋值到数组中,然后进行排版
#pragma mark - Datas
- (void)setExpressionDatas:(NSArray *)expressionDatas
{
_expressionDatas = expressionDatas;// 修改
// 删除之前的控件, 避免重复创建
[self.expressionScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// 设置页数
NSInteger pageCount = (expressionDatas.count + kEachPageEmotions - 1) / kEachPageEmotions;
// self.emotionsPageControl.numberOfPages = pageCount > 1 ? pageCount : 0;
self.expressionPageControl.numberOfPages = pageCount;
// 创建单页表情容器层
for (int i = 0; i < pageCount; i ++)
{
YSWeiBoExpressionEmojiView *emotionsPageView = [[YSWeiBoExpressionEmojiView alloc] init];
// 计算单页的表情范围
NSRange range;
//#define kEmotionPageViewMaxCols 7 // 最大列数
//#define kEmotionPageViewMaxRows 3 // 最大行数
//#define kEachPageEmotions (kEmotionPageViewMaxCols * kEmotionPageViewMaxRows - 1) // 边距
range.location = i * kEachPageEmotions;// 20
// 每次分配后剩余的表情
NSInteger residueEmotions = expressionDatas.count - range.location;// 减去第一页所剩的个数
// 单页表情是否填满
if (residueEmotions >= kEachPageEmotions)
{
range.length = kEachPageEmotions;
}
else
{
range.length = residueEmotions;
}
// 设置单页表情数
emotionsPageView.expressionEmojidatas = [expressionDatas subarrayWithRange:range];
[self.expressionScrollView addSubview:emotionsPageView];
}
// 重新设置子控件 -> 最近表情挤一块
[self setNeedsLayout];
}
获取到使用Frame排布
- (void)layoutSubviews
{
[super layoutSubviews];
NSInteger emotionsCount = self.expressionEmojidatas.count;
CGFloat emotionButtonW = (self.frame.size.width - 2 *kEmotionPageViewMargin) / kEmotionPageViewMaxCols;
CGFloat emotionButtonH = (self.frame.size.height - kEmotionPageViewMargin) / kEmotionPageViewMaxRows;
for (int i = 0; i < emotionsCount; i++)
{
YSWeiBoEmotionButton *emotionButton = self.subviews[i];
emotionButton.backgroundColor = MJRandomColor;
emotionButton.frame = CGRectMake
(kEmotionPageViewMargin + (i % kEmotionPageViewMaxCols) * emotionButtonW,
kEmotionPageViewMargin + (i / kEmotionPageViewMaxCols) * emotionButtonH,
emotionButtonW,
emotionButtonH);
}
}