2023年7月14日发(作者:)
iOSUICollectionView实现卡⽚效果现在使⽤卡⽚效果的app很多,之前公司让实现⼀种卡⽚效果,就写了⼀篇关于实现卡⽚的⽂章。⽂章最后附有demo实现上我选择了使⽤UICollectionView ;⽤UICollectionViewFlowLayout来定制样式;下⾯看看具体实现具体实现1、创建UICollectionView - (void)createCollectionView { CGFloat pading = 0 * SCREEN_WIDTH/375; LHLeftCollocationView * layout = [[LHLeftCollocationView alloc]init]; Direction = UICollectionViewScrollDirectionHorizontal; mLineSpacing = pading; mInteritemSpacing = pading;// UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];// Direction = UICollectionViewScrollDirectionHorizontal; _collectionView3 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen]., imageHeight * SCREEN_RATE) collectionViewLayout:layout]; _ = 33; _urce = self; _te = self; _s = NO; _BounceHorizontal = NO; _BounceVertical = NO; _oundColor = [UIColor grayColor]; _orizontalScrollIndicator = NO; _erticalScrollIndicator = NO; [ addSubview:_collectionView3]; [_collectionView3 registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:collectionViewCell];}2、实现具体代理⽅法 UICollectionViewDelegate,UICollectionViewDataSource- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return ;}- (NSMutableArray *)modelArray { if (!_modelArray) { _modelArray = [NSMutableArray array]; } return _modelArray;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CollModel *infoModel = rray[]; NSLog(@"section:%ld --- row:%ld -----%@",n,,); CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCell forIndexPath:indexPath]; del = infoModel; return cell;}// 返回每个item的⼤⼩- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGFloat CWidth = 80 * SCREEN_RATE; CGFloat CHeight = 80 * SCREEN_RATE; return CGSizeMake(CWidth, CHeight);}#pragma mark - UICollectionViewDelegate点击事件- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ CollModel *infoModel = rray[]; NSLog(@"infoModelArray----%@",);}3、⾃定义UICollectionViewFlowLayoutLHLeftCollocationView.m 实现#import "LHLeftCollocationView.h"@implementation LHLeftCollocationView- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity { CGRect targectRect = CGRectMake(proposedContentOffset.x, 0.0, [UIScreen mainScreen]., [UIScreen mainScreen].); NSArray * attriArray = [super layoutAttributesForElementsInRect:targectRect]; CGFloat horizontalCenterX = proposedContentOffset.x + ([UIScreen mainScreen].); CGFloat offsetAdjustment = CGFLOAT_MAX; for (UICollectionViewLayoutAttributes * layoutAttributes in attriArray) { CGFloat itemHorizontalCenterX = .x; if (fabs(itemHorizontalCenterX-horizontalCenterX) < fabs(offsetAdjustment)) { offsetAdjustment = itemHorizontalCenterX - horizontalCenterX; } } return CGPointMake(proposedContentOffset.x , proposedContentOffset.y);}CGFloat ActiveDistance = 400; //垂直缩放除以系数CGFloat ScaleFactor = 0.50; //缩放系数 越⼤缩放越⼤- (NSArray
cell ⾃定义#import
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689265658a226476.html
评论列表(0条)