实现类似微信表情包横向滚动翻页的功能,运用UICollectionView,自定义UICollectionViewFlowLayout,cell左右排版 ,支持多组Cell实现。

Posted 偶阵雨ss33

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现类似微信表情包横向滚动翻页的功能,运用UICollectionView,自定义UICollectionViewFlowLayout,cell左右排版 ,支持多组Cell实现。相关的知识,希望对你有一定的参考价值。

结合:https://blog.csdn.net/qiuhaozhou/article/details/54582741

下面是我所要的样式的实现的代码:

.h文件如下:

#import <UIKit/UIKit.h> 

@interface JYECircleFlowLayout : UICollectionViewFlowLayout

//  一行中 cell 的个数

@property (nonatomic,assign) NSUInteger itemCountPerRow;

//    一页显示多少行

@property (nonatomic,assign) NSUInteger rowCount; 

/** 列间距 */

@property (nonatomic, assign) CGFloat columnSpacing;

/** 行间距 */

@property (nonatomic, assign) CGFloat rowSpacing;

/** collectionView的内边距 */

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

/** 设置行列间距及collectionView的内边距 */

- (void)setColumnSpacing:(CGFloat)columnSpacing rowSpacing:(CGFloat)rowSpacing edgeInsets:(UIEdgeInsets)edgeInsets;

@end

 

.m文件

#import "JYECircleFlowLayout.h"

 

@interface JYECircleFlowLayout () <UICollectionViewDelegateFlowLayout>

 

@property (strong, nonatomic) NSMutableArray *allAttributes;

 

@end

 

@implementation JYECircleFlowLayout

#pragma mark - Public

- (void)setColumnSpacing:(CGFloat)columnSpacing rowSpacing:(CGFloat)rowSpacing edgeInsets:(UIEdgeInsets)edgeInsets

{

    self.columnSpacing = columnSpacing;

    self.rowSpacing = rowSpacing;

    self.edgeInsets = edgeInsets;

}

 

 

#pragma mark - 重写父类方法

- (instancetype)init

{

    self = [super init];

    if (self) {

 

    }

    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];

    }

}

 

/** 计算collectionView的滚动范围 */

- (CGSize)collectionViewContentSize

{

    NSInteger sections = [self.collectionView numberOfSections];

    return CGSizeMake(JYEScreenWidth*sections, 0);

}

 

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath

{

    NSUInteger item = indexPath.item;

    NSUInteger x;

    NSUInteger y;

    [self targetPositionWithItem:item resultX:&x resultY:&y];

    NSUInteger item2 = [self originItemAtX:x y:y];

    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:(NSUInteger)item resultX:(NSUInteger *)x resultY:(NSUInteger *)y{

    NSUInteger page = item/(self.itemCountPerRow*self.rowCount);

    NSUInteger theX = item % self.itemCountPerRow + page * self.itemCountPerRow;

    NSUInteger theY = item / self.itemCountPerRow - page * self.rowCount;

    if (x != NULL) {

        *x = theX;

    }

    if (y != NULL) {

        *y = theY;

    }

}

 

// 根据偏移量计算item

- (NSUInteger)originItemAtX:(NSUInteger)x y:(NSUInteger)y{

    NSUInteger item = x * self.rowCount + y;

    return item;

}

@end

 

以上是关于实现类似微信表情包横向滚动翻页的功能,运用UICollectionView,自定义UICollectionViewFlowLayout,cell左右排版 ,支持多组Cell实现。的主要内容,如果未能解决你的问题,请参考以下文章

关于scrollview的无限滚动效果实现

jsp聊天室实现发送表情功能

IM项目中的自定义小表情实现

xamarin android开发 怎么实现左,右滑动翻页的效果的

微信小程序实现横向滚动文字

微信打字出表情包怎么设置 微信打字出表情包如何设置