You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

184 lines
6.8 KiB

//
// XHEmotionManagerView.m
// MessageDisplayExample
//
// Created by HUAJIE-1 on 14-5-3.
// Copyright (c) 2014年 嗨,我是曾宪华(@xhzengAIB),曾加入YY Inc.担任高级移动开发工程师,拍立秀App联合创始人,热衷于简洁、而富有理性的事物 QQ:543413507 主页:http://zengxianhua.com All rights reserved.
//
#import "XHEmotionManagerView.h"
#import "XHEmotionSectionBar.h"
#import "XHEmotionCollectionViewCell.h"
#import "XHEmotionCollectionViewFlowLayout.h"
@interface XHEmotionManagerView () <UICollectionViewDelegate, UICollectionViewDataSource, XHEmotionSectionBarDelegate>
/**
* 显示表情的collectView控件
*/
@property (nonatomic, weak) UICollectionView *emotionCollectionView;
/**
* 显示页码的控件
*/
@property (nonatomic, weak) UIPageControl *emotionPageControl;
/**
* 管理多种类别gif表情的滚动试图
*/
@property (nonatomic, weak) XHEmotionSectionBar *emotionSectionBar;
/**
* 当前选择了哪类gif表情标识
*/
@property (nonatomic, assign) NSInteger selectedIndex;
/**
* 配置默认控件
*/
- (void)setup;
@end
@implementation XHEmotionManagerView
- (void)reloadData {
NSInteger numberOfEmotionManagers = [self.dataSource numberOfEmotionManagers];
if (!numberOfEmotionManagers) {
return ;
}
self.emotionSectionBar.emotionManagers = [self.dataSource emotionManagersAtManager];
[self.emotionSectionBar reloadData];
XHEmotionManager *emotionManager = [self.dataSource emotionManagerForColumn:self.selectedIndex];
NSInteger numberOfEmotions = emotionManager.emotions.count;
self.emotionPageControl.numberOfPages = (numberOfEmotions / (kXHEmotionPerRowItemCount * 2) + (numberOfEmotions % (kXHEmotionPerRowItemCount * 2) ? 1 : 0));
[self.emotionCollectionView reloadData];
}
#pragma mark - Life cycle
- (void)setup {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.backgroundColor = [UIColor colorWithWhite:0.961 alpha:1.000];
self.isShowEmotionStoreButton = YES;
if (!_emotionCollectionView) {
UICollectionView *emotionCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) - kXHEmotionPageControlHeight - kXHEmotionSectionBarHeight) collectionViewLayout:[[XHEmotionCollectionViewFlowLayout alloc] init]];
emotionCollectionView.backgroundColor = self.backgroundColor;
[emotionCollectionView registerClass:[XHEmotionCollectionViewCell class] forCellWithReuseIdentifier:kXHEmotionCollectionViewCellIdentifier];
emotionCollectionView.showsHorizontalScrollIndicator = NO;
emotionCollectionView.showsVerticalScrollIndicator = NO;
[emotionCollectionView setScrollsToTop:NO];
emotionCollectionView.pagingEnabled = YES;
emotionCollectionView.delegate = self;
emotionCollectionView.dataSource = self;
[self addSubview:emotionCollectionView];
self.emotionCollectionView = emotionCollectionView;
}
if (!_emotionPageControl) {
UIPageControl *emotionPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.emotionCollectionView.frame), CGRectGetWidth(self.bounds), kXHEmotionPageControlHeight)];
emotionPageControl.currentPageIndicatorTintColor = [UIColor colorWithWhite:0.471 alpha:1.000];
emotionPageControl.pageIndicatorTintColor = [UIColor colorWithWhite:0.678 alpha:1.000];
emotionPageControl.backgroundColor = self.backgroundColor;
emotionPageControl.hidesForSinglePage = YES;
emotionPageControl.defersCurrentPageDisplay = YES;
[self addSubview:emotionPageControl];
self.emotionPageControl = emotionPageControl;
}
if (!_emotionSectionBar) {
XHEmotionSectionBar *emotionSectionBar = [[XHEmotionSectionBar alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.emotionPageControl.frame), CGRectGetWidth(self.bounds), kXHEmotionSectionBarHeight) showEmotionStoreButton:self.isShowEmotionStoreButton];
emotionSectionBar.delegate = self;
emotionSectionBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
emotionSectionBar.backgroundColor = [UIColor colorWithWhite:0.886 alpha:1.000];
[self addSubview:emotionSectionBar];
self.emotionSectionBar = emotionSectionBar;
}
}
- (void)awakeFromNib {
[self setup];
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
return self;
}
- (void)dealloc {
self.emotionPageControl = nil;
self.emotionSectionBar = nil;
self.emotionCollectionView.delegate = nil;
self.emotionCollectionView.dataSource = nil;
self.emotionCollectionView = nil;
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
if (newSuperview) {
[self reloadData];
}
}
#pragma mark - XHEmotionSectionBar Delegate
- (void)didSelecteEmotionManager:(XHEmotionManager *)emotionManager atSection:(NSInteger)section {
self.selectedIndex = section;
self.emotionPageControl.currentPage = 0;
[self reloadData];
}
#pragma mark - UIScrollView delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//每页宽度
CGFloat pageWidth = scrollView.frame.size.width;
//根据当前的坐标与页宽计算当前页码
NSInteger currentPage = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth)+1;
[self.emotionPageControl setCurrentPage:currentPage];
}
#pragma UICollectionView DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
XHEmotionManager *emotionManager = [self.dataSource emotionManagerForColumn:self.selectedIndex];
NSInteger count = emotionManager.emotions.count;
return count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XHEmotionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kXHEmotionCollectionViewCellIdentifier forIndexPath:indexPath];
XHEmotionManager *emotionManager = [self.dataSource emotionManagerForColumn:self.selectedIndex];
cell.emotion = emotionManager.emotions[indexPath.row];
return cell;
}
#pragma mark - UICollectionView delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if ([self.delegate respondsToSelector:@selector(didSelecteEmotion:atIndexPath:)]) {
XHEmotionManager *emotionManager = [self.dataSource emotionManagerForColumn:indexPath.section];
[self.delegate didSelecteEmotion:emotionManager.emotions[indexPath.row] atIndexPath:indexPath];
}
}
@end