// // XHEmotionManagerView.m // MessageDisplayExample // // Created by HUAJIE-1 on 14-5-3. // Copyright (c) 2014年 曾宪华 开发团队(http://iyilunba.com ) 本人QQ:543413507 本人QQ群(142557668). All rights reserved. // #import "XHEmotionManagerView.h" #import "XHEmotionSectionBar.h" #import "XHEmotionCollectionViewCell.h" #import "XHEmotionCollectionViewFlowLayout.h" #import "WSLWaterFlowLayout.h" @interface XHEmotionManagerView () @property (nonatomic, assign) WSLWaterFlowLayoutStyle flowLayoutStyle; /** * 显示表情的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 = KKClearColor; self.isShowEmotionStoreButton = NO; if (!_emotionCollectionView) { WSLWaterFlowLayout *FlowLayout = [[WSLWaterFlowLayout alloc] init]; FlowLayout.delegate = self; FlowLayout.flowLayoutStyle = self.flowLayoutStyle; UICollectionView *emotionCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, CGRectGetHeight(self.bounds)) collectionViewLayout:FlowLayout]; emotionCollectionView.backgroundColor = KKClearColor; [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 #pragma mark - WSLWaterFlowLayoutDelegate //返回每个item大小 - (CGSize)waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake((ScreenWidth-80)/4, (ScreenWidth-80)/4); } /** 头视图Size */ -(CGSize )waterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout sizeForHeaderViewInSection:(NSInteger)section { return CGSizeMake(ScreenWidth, 0.1); } /** 列数*/ -(CGFloat)columnCountInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout { return 4; } /** 列间距*/ -(CGFloat)columnMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout { return 16; } /** 行间距*/ -(CGFloat)rowMarginInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout { return 16; } /** 边缘之间的间距*/ -(UIEdgeInsets)edgeInsetInWaterFlowLayout:(WSLWaterFlowLayout *)waterFlowLayout { return UIEdgeInsetsMake(0, 16, 0, 16); } - (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