From 3e8437ae559487362fae3525beb79c534c213a51 Mon Sep 17 00:00:00 2001 From: 单军华 Date: Thu, 12 Jul 2018 13:44:34 +0800 Subject: [PATCH] bug修复和功能优化 --- screendisplay/Pods/HMEmoticon/表情键盘/Emoticon/HMEmoticonInputView.m | 219 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 219 insertions(+), 0 deletions(-) diff --git "a/screendisplay/Pods/HMEmoticon/\350\241\250\346\203\205\351\224\256\347\233\230/Emoticon/HMEmoticonInputView.m" "b/screendisplay/Pods/HMEmoticon/\350\241\250\346\203\205\351\224\256\347\233\230/Emoticon/HMEmoticonInputView.m" new file mode 100755 index 0000000..a333684 --- /dev/null +++ "b/screendisplay/Pods/HMEmoticon/\350\241\250\346\203\205\351\224\256\347\233\230/Emoticon/HMEmoticonInputView.m" @@ -0,0 +1,219 @@ +// +// HMEmoticonInputView.m +// ������������ +// +// Created by ������ on 16/3/3. +// Copyright �� 2016��� itcast. All rights reserved. +// + +#import "HMEmoticonInputView.h" +#import "HMEmoticonToolbar.h" +#import "UIImage+HMEmoticon.h" +#import "HMEmoticonManager.h" +#import "HMEmoticonCell.h" + +/// ������ Cell ��������������������� +NSString *const HMEmoticonCellIdentifier = @"HMEmoticonCellIdentifier"; + +#pragma mark - ������������������ +@interface HMEmoticonKeyboardLayout : UICollectionViewFlowLayout + +@end + +@implementation HMEmoticonKeyboardLayout + +- (void)prepareLayout { + [super prepareLayout]; + + self.itemSize = self.collectionView.bounds.size; + self.minimumInteritemSpacing = 0; + self.minimumLineSpacing = 0; + self.scrollDirection = UICollectionViewScrollDirectionHorizontal; + + self.collectionView.pagingEnabled = YES; + self.collectionView.showsHorizontalScrollIndicator = NO; + self.collectionView.bounces = NO; +} + +@end + +#pragma mark - ������������������ +@interface HMEmoticonInputView() <UICollectionViewDataSource, UICollectionViewDelegate, HMEmoticonToolbarDelegate, HMEmoticonCellDelegate> + +@end + +@implementation HMEmoticonInputView { + UICollectionView *_collectionView; + HMEmoticonToolbar *_toolbar; + UIPageControl *_pageControl; + + void (^_selectedEmoticonCallBack)(HMEmoticon * _Nullable, BOOL); +} + +#pragma mark - ������������ +- (instancetype)initWithSelectedEmoticon:(void (^)(HMEmoticon * _Nullable, BOOL))selectedEmoticon { + CGRect frame = [UIScreen mainScreen].bounds; + frame.size.height = 216; + + self = [super initWithFrame:frame]; + if (self) { + _selectedEmoticonCallBack = selectedEmoticon; + + [self prepareUI]; + + NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:1]; + [_collectionView scrollToItemAtIndexPath:indexPath + atScrollPosition:UICollectionViewScrollPositionLeft + animated:NO]; + [self updatePageControlWithIndexPath:indexPath]; + [_toolbar selectSection:indexPath.section]; + } + return self; +} + +- (instancetype)initWithFrame:(CGRect)frame { + NSAssert(NO, @"��������� initWithSelectedEmoticon: ���������������������������"); + return nil; +} + +#pragma mark - HMEmoticonToolbarDelegate +- (void)emoticonToolbarDidSelectSection:(NSInteger)section { + NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:section]; + + [_collectionView scrollToItemAtIndexPath:indexPath + atScrollPosition:UICollectionViewScrollPositionLeft + animated:NO]; + [self updatePageControlWithIndexPath:indexPath]; +} + +#pragma mark - HMEmoticonCellDelegate +- (void)emoticonCellDidSelectedEmoticon:(HMEmoticon *)emoticon isRemoved:(BOOL)isRemoved { + if (_selectedEmoticonCallBack != nil) { + _selectedEmoticonCallBack(emoticon, isRemoved); + } + + /// ������������������������ + if (emoticon != nil) { + [[HMEmoticonManager sharedManager] addRecentEmoticon:emoticon]; + + // ��������������������������������������������������������������������������� + if ([_collectionView indexPathsForVisibleItems].firstObject.section != 0) { + [_collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:0]]]; + } + } +} + +#pragma mark - UICollectionViewDataSource, UICollectionViewDelegate +- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { + return [HMEmoticonManager sharedManager].packages.count; +} + +- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { + return [[HMEmoticonManager sharedManager] numberOfPagesInSection:section]; +} + +- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { + + HMEmoticonCell *cell = [collectionView + dequeueReusableCellWithReuseIdentifier:HMEmoticonCellIdentifier + forIndexPath:indexPath]; + + cell.emoticons = [[HMEmoticonManager sharedManager] emoticonsWithIndexPath:indexPath]; + cell.indexPath = indexPath; + cell.delegate = self; + + return cell; +} + +#pragma mark - UIScrollView Delegate +- (void)scrollViewDidScroll:(UIScrollView *)scrollView { + CGPoint center = scrollView.center; + center.x += scrollView.contentOffset.x; + + NSArray *indexPaths = [_collectionView indexPathsForVisibleItems]; + + NSIndexPath *targetPath = nil; + for (NSIndexPath *indexPath in indexPaths) { + UICollectionViewCell *cell = [_collectionView cellForItemAtIndexPath:indexPath]; + + if (CGRectContainsPoint(cell.frame, center)) { + targetPath = indexPath; + break; + } + } + + if (targetPath != nil) { + [self updatePageControlWithIndexPath:targetPath]; + [_toolbar selectSection:targetPath.section]; + } +} + +- (void)updatePageControlWithIndexPath:(NSIndexPath *)indexPath { + _pageControl.numberOfPages = [[HMEmoticonManager sharedManager] numberOfPagesInSection:indexPath.section]; + _pageControl.currentPage = indexPath.item; +} + +#pragma mark - ������������ +- (void)prepareUI { + // 1. ������������������ + self.backgroundColor = [UIColor colorWithPatternImage:[UIImage hm_imageNamed:@"emoticon_keyboard_background"]]; + + // 2. ��������������� + _toolbar = [[HMEmoticonToolbar alloc] init]; + [self addSubview:_toolbar]; + + // ��������������������� + CGFloat toolbarHeight = 42; + CGRect toolbarRect = self.bounds; + toolbarRect.origin.y = toolbarRect.size.height - toolbarHeight; + toolbarRect.size.height = toolbarHeight; + _toolbar.frame = toolbarRect; + + _toolbar.delegate = self; + + // 3. ������ collectionView + CGRect collectionViewRect = self.bounds; + collectionViewRect.size.height -= toolbarHeight; + _collectionView = [[UICollectionView alloc] + initWithFrame:collectionViewRect + collectionViewLayout:[[HMEmoticonKeyboardLayout alloc] init]]; + [self addSubview:_collectionView]; + + // ������ collectionView + _collectionView.backgroundColor = [UIColor clearColor]; + + _collectionView.dataSource = self; + _collectionView.delegate = self; + + [_collectionView registerClass:[HMEmoticonCell class] forCellWithReuseIdentifier:HMEmoticonCellIdentifier]; + + // 4. ������������ + _pageControl = [[UIPageControl alloc] init]; + [self addSubview:_pageControl]; + + // ������������������ + _pageControl.hidesForSinglePage = YES; + _pageControl.userInteractionEnabled = NO; + + [_pageControl setValue:[UIImage hm_imageNamed:@"compose_keyboard_dot_selected"] forKey:@"_currentPageImage"]; + [_pageControl setValue:[UIImage hm_imageNamed:@"compose_keyboard_dot_normal"] forKey:@"_pageImage"]; + + // ������������ + _pageControl.translatesAutoresizingMaskIntoConstraints = NO; + [self addConstraint:[NSLayoutConstraint constraintWithItem:_pageControl + attribute:NSLayoutAttributeCenterX + relatedBy:NSLayoutRelationEqual + toItem:self + attribute:NSLayoutAttributeCenterX + multiplier:1.0 + constant:0.0]]; + [self addConstraint:[NSLayoutConstraint constraintWithItem:_pageControl + attribute:NSLayoutAttributeBottom + relatedBy:NSLayoutRelationEqual + toItem:_toolbar + attribute:NSLayoutAttributeTop + multiplier:1.0 + constant:0.0]]; +} + +@end -- Gitblit v1.8.0