当前位置: 首页 > news >正文

网站模板大全下载湖南关键词排名推广

网站模板大全下载,湖南关键词排名推广,百度手机网页,科技网络网站建设iOS开发-聊天emoji表情与自定义动图表情左右滑动控件 之前开发中遇到需要实现聊天emoji表情与自定义动图表情左右滑动控件。使用UICollectionView实现。 一、效果图 二、实现代码 UICollectionView是一种类似于UITableView但又比UITableView功能更强大、更灵活的视图&#x…

iOS开发-聊天emoji表情与自定义动图表情左右滑动控件

之前开发中遇到需要实现聊天emoji表情与自定义动图表情左右滑动控件。使用UICollectionView实现。

一、效果图

在这里插入图片描述

二、实现代码

UICollectionView是一种类似于UITableView但又比UITableView功能更强大、更灵活的视图,这是源于它将UICollectionView对cell的布局交给了UICollectionViewLayout,而且允许用户自定义layout来进行布局。

2.1 UICollectionView初始化

INEmotionView.h

@interface INEmotionView : UIView@property (nonatomic, weak) id delegate;@property (nonatomic, strong) INEmotionFlowLayout *flowLayout;@property (nonatomic, strong) UICollectionView *collectionView;@property (nonatomic, strong) UIPageControl *pageControl;- (id)initWithFrame:(CGRect)frame;@end

INEmotionView.m

#import "INEmotionView.h"
#import "UIColor+Addition.h"static CGFloat kCollectionHeight = 260;@implementation INEmotionView- (id)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {self.backgroundColor = [UIColor colorWithHexString:@"efeff4"];self.flowLayout =[[INEmotionFlowLayout alloc] init];self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;self.collectionView = [[UICollectionView alloc]initWithFrame:frame collectionViewLayout:self.flowLayout];self.collectionView.backgroundColor = [UIColor clearColor];self.collectionView.scrollEnabled = YES;self.collectionView.pagingEnabled = YES;self.collectionView.showsVerticalScrollIndicator = NO;self.collectionView.showsHorizontalScrollIndicator = NO;self.collectionView.userInteractionEnabled = YES;self.collectionView.exclusiveTouch = YES;[self addSubview:self.collectionView];self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectZero];self.pageControl.backgroundColor = [UIColor clearColor];self.pageControl.currentPage = 0;self.pageControl.numberOfPages = 0;self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];self.pageControl.pageIndicatorTintColor = [UIColor greenColor];[self addSubview:self.pageControl];}return self;
}- (id)init {return [self initWithFrame:CGRectZero];
}- (void)layoutSubviews {[super layoutSubviews];self.collectionView.frame = CGRectMake(0.0, (CGRectGetHeight(self.bounds) - kCollectionHeight)/2, CGRectGetWidth(self.bounds), kCollectionHeight);self.pageControl.frame = CGRectMake(0.0, CGRectGetMaxY(self.collectionView.frame), CGRectGetWidth(self.bounds), 50);
}- (void)setDelegate:(id)delegate {_delegate = delegate;self.collectionView.delegate = delegate;self.collectionView.dataSource = delegate;
}@end

2.2 UICollectionView实现控件

emoji表情与自定义动图表情左右切换,需要根据拆分多个section,UICollectionView的datasource
我这里使用的是5个分组,每个分组的数量如下。

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {return 5;
}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {NSInteger sectionNumber = 60;switch (section) {case 0:sectionNumber = 72;break;case 1:sectionNumber = 8;break;case 2:sectionNumber = 16;break;case 3:sectionNumber = 24;break;case 4:sectionNumber = 32;break;default:break;}return sectionNumber;
}

UICollectionView左右切换,需要将pagingEnabled设置为YES。

界面上用到了UIPageControl,由于UICollectionView继承UIScrollView。不同section的页码不一样,所以在scrollViewDidEndDecelerating方法中更改UIPageControl的numberOfPages与currentPage。

/** 手指滑动屏幕时,视图停止滚动会调用此方法 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {NSInteger collectionPage = scrollView.contentOffset.x/scrollView.frame.size.width;NSInteger aIndex = 0;NSInteger sectionIndex = 0;for (NSInteger index = 0; index < self.sectionPageNumbers.count; index ++) {NSString *sectionPage = [self.sectionPageNumbers objectAtIndex:index];aIndex = aIndex+[sectionPage integerValue];if (collectionPage >= 0 && collectionPage < aIndex) {sectionIndex = index;break;}}NSString *sectionCount = [self.sectionPageNumbers objectAtIndex:sectionIndex];NSLog(@"sectionCount:%@",sectionCount);NSInteger preCount = 0;for (NSInteger i = 0; i < sectionIndex; i++) {NSString *sectionPage = [self.sectionPageNumbers objectAtIndex:i];preCount = preCount + sectionPage.integerValue;}NSInteger sectionPageCount = sectionCount.integerValue;NSInteger sectionCurPage = collectionPage - preCount;NSLog(@"sectionPageCount:%ld",(long)sectionPageCount);NSLog(@"sectionCurPage:%ld",(long)sectionCurPage);self.emojiView.pageControl.numberOfPages = sectionPageCount;self.emojiView.pageControl.currentPage = sectionCurPage;
}

整体使用UICollectionView代理delegate方法代码如下

#import "INEmotionPresenter.h"#define kCustomEmotionScreenWidth [UIScreen mainScreen].bounds.size.width@interface INCollectionSectionPageRange : NSObject@property (nonatomic, assign) NSString *beginNumber;
@property (nonatomic, assign) NSString *endNumber;@end@implementation INCollectionSectionPageRange@end@implementation INEmotionPresenter#pragma mark - 注册cell
/**注册cell*/
- (void)registerCollectionCell {[self.emojiView.collectionView registerClass:[INEmotionSystemEmojiCell class] forCellWithReuseIdentifier:kEmotionEmojiSystemIdentifier];[self.emojiView.collectionView registerClass:[INEmotionCustomEmojiCell class] forCellWithReuseIdentifier:kEmotionEmojiCustomIdentifier];
}#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {UIEdgeInsets contentInset = collectionView.contentInset;NSInteger columnCount = 4;NSInteger rowCount = 2;if (indexPath.section == 0) {columnCount = 8;rowCount = 3;}CGFloat w = (CGRectGetWidth(collectionView.bounds) - contentInset.left - contentInset.right)/ columnCount;CGFloat h = (CGRectGetHeight(collectionView.bounds) - contentInset.top - contentInset.bottom)/rowCount;return CGSizeMake(w, h);
}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {return 0.0;
}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {return 0.0;
}#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {return 5;
}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {NSInteger sectionNumber = 60;switch (section) {case 0:sectionNumber = 72;break;case 1:sectionNumber = 8;break;case 2:sectionNumber = 16;break;case 3:sectionNumber = 24;break;case 4:sectionNumber = 32;break;default:break;}return sectionNumber;
}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {if (indexPath.section == 0) {//emoji表情INEmotionSystemEmojiCell *cell = (INEmotionSystemEmojiCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kEmotionEmojiSystemIdentifier forIndexPath:indexPath];cell.emojiLabel.text = @"";cell.emojiLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.item];cell.cellNumber = [NSString stringWithFormat:@"%ld",(long)indexPath.item];return cell;}//自定义表情贴图INEmotionCustomEmojiCell *cell = (INEmotionCustomEmojiCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kEmotionEmojiCustomIdentifier forIndexPath:indexPath];cell.cellNumber = [NSString stringWithFormat:@"%ld",(long)indexPath.item];return cell;
}- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {}- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {NSInteger collectionPage = scrollView.contentOffset.x/scrollView.frame.size.width;NSInteger aIndex = 0;NSInteger sectionIndex = 0;for (NSInteger index = 0; index < self.sectionPageNumbers.count; index ++) {NSString *sectionPage = [self.sectionPageNumbers objectAtIndex:index];aIndex = aIndex+[sectionPage integerValue];if (collectionPage >= 0 && collectionPage < aIndex) {sectionIndex = index;break;}}NSString *sectionCount = [self.sectionPageNumbers objectAtIndex:sectionIndex];NSLog(@"sectionCount:%@",sectionCount);NSInteger preCount = 0;for (NSInteger i = 0; i < sectionIndex; i++) {NSString *sectionPage = [self.sectionPageNumbers objectAtIndex:i];preCount = preCount + sectionPage.integerValue;}NSInteger sectionPageCount = sectionCount.integerValue;NSInteger sectionCurPage = collectionPage - preCount;NSLog(@"sectionPageCount:%ld",(long)sectionPageCount);NSLog(@"sectionCurPage:%ld",(long)sectionCurPage);self.emojiView.pageControl.numberOfPages = sectionPageCount;self.emojiView.pageControl.currentPage = sectionCurPage;
}#pragma mark - SETTER/GETTER
- (NSMutableArray *)sectionPageNumbers {if (!_sectionPageNumbers) {_sectionPageNumbers = [NSMutableArray arrayWithCapacity:0];[_sectionPageNumbers addObject:@"3"];[_sectionPageNumbers addObject:@"1"];[_sectionPageNumbers addObject:@"2"];[_sectionPageNumbers addObject:@"3"];[_sectionPageNumbers addObject:@"4"];}return _sectionPageNumbers;
}- (INEmotionConfig *)emojiConfig {if (!_emojiConfig) {_emojiConfig = [[INEmotionConfig alloc] init];}return _emojiConfig;
}- (INEmotionInteractor *)emojiInteractor {if (!_emojiInteractor) {_emojiInteractor = [[INEmotionInteractor alloc] init];}return _emojiInteractor;
}- (INEmotionView *)emojiView {if (!_emojiView) {_emojiView = [[INEmotionView alloc] initWithFrame:CGRectZero];}return _emojiView;
}@end

2.3 实现表情的排列UICollectionViewFlowLayout

由于emoji表情需要3行8列,自定义贴图表情需要2行4列排列。我这里实现一下UICollectionViewFlowLayout
要在layoutAttributesForItemAtIndexPath中区分,如果section为0,则为3行8列;否则为2行4列。

INEmotionFlowLayout.h

#import <UIKit/UIKit.h>@interface INEmotionFlowLayout : UICollectionViewFlowLayout- (UICollectionViewLayoutAttributes *)customLayoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;@end

INEmotionFlowLayout.m

#import "INEmotionFlowLayout.h"#define kLayScreenWidth [UIScreen mainScreen].bounds.size.width@interface INEmotionFlowLayout () <UICollectionViewDelegateFlowLayout>@property (strong, nonatomic) NSMutableArray *allAttributes;@property (nonatomic, assign) NSInteger currentRow;@property (nonatomic, assign) NSInteger currentCol;@end@implementation INEmotionFlowLayout-(instancetype)init
{if (self = [super init]){}return self;
}- (void)prepareLayout
{[super prepareLayout];self.allAttributes = [NSMutableArray array];NSInteger sections = [self.collectionView numberOfSections];for (int i = 0; i < sections; i++){NSMutableArray * tmpArray = [NSMutableArray array];NSUInteger count = [self.collectionView numberOfItemsInSection:i];for (NSUInteger j = 0; j<count; j++) {NSIndexPath *indexPath = [NSIndexPath indexPathForItem:j inSection:i];UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];[tmpArray addObject:attributes];}[self.allAttributes addObject:tmpArray];}
}- (CGSize)collectionViewContentSize
{return [super collectionViewContentSize];
}- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{NSUInteger item = indexPath.item;NSUInteger x;NSUInteger y;[self targetPositionWithItem:indexPath resultX:&x resultY:&y];NSUInteger item2 = [self originItemAtX:x y:y indexPath:indexPath];NSIndexPath *theNewIndexPath = [NSIndexPath indexPathForItem:item2 inSection:indexPath.section];UICollectionViewLayoutAttributes *theNewAttr = [super layoutAttributesForItemAtIndexPath:theNewIndexPath];theNewAttr.indexPath = indexPath;return theNewAttr;
}- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{NSArray *attributes = [super layoutAttributesForElementsInRect:rect];NSMutableArray *tmp = [NSMutableArray array];for (UICollectionViewLayoutAttributes *attr in attributes) {for (NSMutableArray *attributes in self.allAttributes){for (UICollectionViewLayoutAttributes *attr2 in attributes) {if (attr.indexPath.item == attr2.indexPath.item) {[tmp addObject:attr2];break;}}}}return tmp;
}- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{return YES;
}// 根据 item 计算目标item的位置
// x 横向偏移  y 竖向偏移
- (void)targetPositionWithItem:(NSIndexPath *)indexPathresultX:(NSUInteger *)xresultY:(NSUInteger *)y
{NSInteger columnCount = 4;NSInteger rowCount = 2;if (indexPath.section == 0) {columnCount = 8;rowCount = 3;}NSInteger item = indexPath.item;NSUInteger page = item/(columnCount*rowCount);NSUInteger theX = item % columnCount + page * columnCount;NSUInteger theY = item / columnCount - page * rowCount;if (x != NULL) {*x = theX;}if (y != NULL) {*y = theY;}}// 根据偏移量计算item
- (NSUInteger)originItemAtX:(NSUInteger)xy:(NSUInteger)yindexPath:(NSIndexPath *)indexPath
{NSInteger columnCount = 4;NSInteger rowCount = 2;if (indexPath.section == 0) {columnCount = 8;rowCount = 3;}NSUInteger item = x * rowCount + y;return item;
}- (UICollectionViewLayoutAttributes *)customLayoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {return [self layoutAttributesForItemAtIndexPath:indexPath];
}@end

2.4 emoji表情的UICollectionViewCell

继承UICollectionViewCell实现INEmotionSystemEmojiCell表情

INEmotionSystemEmojiCell.h

#import <UIKit/UIKit.h>
#import "UIColor+Addition.h"@interface INEmotionSystemEmojiCell : UICollectionViewCell@property (nonatomic, strong) UIImageView *emojiImageView;@property (nonatomic, strong) UILabel *emojiLabel;/**cell的序号*/
@property (nonatomic, strong) NSString *cellNumber;/**按照序号,从小到大@param cell cell@return 排序*/
- (NSComparisonResult)sortAscCells:(INEmotionSystemEmojiCell *)cell;@end

INEmotionSystemEmojiCell.m

#import "INEmotionSystemEmojiCell.h"static CGFloat kEmotionEmojiSize = 40.0;@implementation INEmotionSystemEmojiCell- (instancetype)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {self.emojiImageView = [[UIImageView alloc] initWithFrame:CGRectMake((CGRectGetWidth(self.bounds) - kEmotionEmojiSize)/2, (CGRectGetHeight(self.bounds) - kEmotionEmojiSize)/2, kEmotionEmojiSize, kEmotionEmojiSize)];self.emojiImageView.backgroundColor = [UIColor randomColor];self.emojiImageView.layer.cornerRadius = kEmotionEmojiSize/2;self.emojiImageView.layer.masksToBounds = YES;[self addSubview:self.emojiImageView];self.emojiLabel = [[UILabel alloc] initWithFrame:CGRectMake((CGRectGetWidth(self.bounds) - kEmotionEmojiSize)/2, (CGRectGetHeight(self.bounds) - kEmotionEmojiSize)/2, kEmotionEmojiSize, kEmotionEmojiSize)];self.emojiLabel.textColor = [UIColor grayColor];self.emojiLabel.textAlignment = NSTextAlignmentCenter;[self addSubview:self.emojiLabel];}return self;
}- (void)layoutSubviews {[super layoutSubviews];self.emojiImageView.frame = CGRectMake((CGRectGetWidth(self.bounds) - kEmotionEmojiSize)/2, (CGRectGetHeight(self.bounds) - kEmotionEmojiSize)/2, kEmotionEmojiSize, kEmotionEmojiSize);self.emojiLabel.frame = CGRectMake((CGRectGetWidth(self.bounds) - kEmotionEmojiSize)/2, (CGRectGetHeight(self.bounds) - kEmotionEmojiSize)/2, kEmotionEmojiSize, kEmotionEmojiSize);
}/**按照序号,从小到大@param cell cell@return 排序*/
- (NSComparisonResult)sortAscCells:(INEmotionSystemEmojiCell *)cell {//先按照时间排序NSComparisonResult result = [self.cellNumber compare:cell.cellNumber];return  result;
}@end

2.5 自定义贴图表情的UICollectionViewCell

INEmotionCustomEmojiCell.h

#import <UIKit/UIKit.h>
#import "UIColor+Addition.h"@interface INEmotionCustomEmojiCell : UICollectionViewCell@property (nonatomic, strong) UIImageView *emojiImageView;/**cell的序号*/
@property (nonatomic, strong) NSString *cellNumber;/**按照序号,从小到大@param cell cell@return 排序*/
- (NSComparisonResult)sortAscCells:(INEmotionCustomEmojiCell *)cell;@end

INEmotionCustomEmojiCell.m

#import "INEmotionCustomEmojiCell.h"static CGFloat kCustomEmotionEmojiSize = 80.0;@implementation INEmotionCustomEmojiCell- (instancetype)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {self.emojiImageView = [[UIImageView alloc] initWithFrame:CGRectMake((CGRectGetWidth(self.bounds) - kCustomEmotionEmojiSize)/2, (CGRectGetHeight(self.bounds) - kCustomEmotionEmojiSize)/2, kCustomEmotionEmojiSize, kCustomEmotionEmojiSize)];self.emojiImageView.backgroundColor = [UIColor randomColor];self.emojiImageView.layer.cornerRadius = 4;self.emojiImageView.layer.masksToBounds = YES;[self addSubview:self.emojiImageView];}return self;
}- (void)layoutSubviews {[super layoutSubviews];self.emojiImageView.frame = CGRectMake((CGRectGetWidth(self.bounds) - kCustomEmotionEmojiSize)/2, (CGRectGetHeight(self.bounds) - kCustomEmotionEmojiSize)/2, kCustomEmotionEmojiSize, kCustomEmotionEmojiSize);
}/**按照序号,从小到大@param cell cell@return 排序*/
- (NSComparisonResult)sortAscCells:(INEmotionCustomEmojiCell *)cell {//先按照时间排序NSComparisonResult result = [self.cellNumber compare:cell.cellNumber];return  result;
}@end

至此整个效果的代码实现完了。

三、小结

iOS开发-聊天emoji表情与自定义动图表情左右滑动控件.使用UICollectionView实现。

学习记录,每天不停进步。

http://www.shuangfujiaoyu.com/news/45591.html

相关文章:

  • 成都公司网页制作电话网站内容优化方法
  • 南京模板建网站哪家好东莞做好网络推广
  • 做网站教学视频搜索引擎营销的方法不包括
  • 厦门网站建设方案服务百度权重4网站值多少钱
  • 优秀网站建设设计百度浏览官网
  • 6黄页网站建设百度竞价排名叫什么
  • 做网站用哪个预装系统网络营销服务有哪些
  • 哈尔滨网站建设专卖电商运营培训课程
  • 北京著名网站建设公司千度seo
  • 毕业设计做网站low站长工具国产
  • iis 网站访问权限 设置云南网络推广seo代理公司
  • 怎么提高网站的转化率怀化网络推广
  • 湘潭网站建设 诚信磐石网络关键词优化报价推荐
  • php搭建网站软件下载seo资料网
  • 免费的好网站淘宝怎样优化关键词
  • 做网站建设费用公司网站怎么做
  • 如何借用别人网站做模板百度如何推广广告
  • 宝安网站制作哪家强软文标题写作技巧
  • 网站seo 工具博客推广工具
  • 鄞州区优秀营销型网站建设首选制作网站平台
  • 网站办公室文化建设软文代发代理
  • 配置 tomcat 做网站品牌营销策划公司哪家好
  • 国外做问卷调查的网站seo网站推广经理
  • 有云服务器和域名怎么做网站长沙优化排名推广
  • 电子工程建设深圳网站建设专业乐云seo
  • 北京网站优化合作网上开店如何推广自己的网店
  • 企业做网站的费用如果做账seo网站优化服务商
  • 网站建设难不难哪些网站可以发广告
  • 酒类网站建设方案公司网络推广网站
  • 中文域名注册 .网站东莞公司网上推广