亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

iOS UICollectionView實現卡片效果

瀏覽:4日期:2022-09-17 09:42:42

現在使用卡片效果的app很多,之前公司讓實現一種卡片效果,就寫了一篇關于實現卡片的文章。文章最后附有demo

實現上我選擇了使用UICollectionView ;用UICollectionViewFlowLayout來定制樣式;下面看看具體實現

具體實現

1、創建UICollectionView

- (void)createCollectionView { CGFloat pading = 0 * SCREEN_WIDTH/375; LHLeftCollocationView * layout = [[LHLeftCollocationView alloc]init]; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; layout.minimumLineSpacing = pading; layout.minimumInteritemSpacing = pading;// UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];// layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _collectionView3 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, imageHeight * SCREEN_RATE) collectionViewLayout:layout]; _collectionView3.tag = 33; _collectionView3.dataSource = self; _collectionView3.delegate = self; _collectionView3.bounces = NO; _collectionView3.alwaysBounceHorizontal = NO; _collectionView3.alwaysBounceVertical = NO; _collectionView3.backgroundColor = [UIColor grayColor]; _collectionView3.showsHorizontalScrollIndicator = NO; _collectionView3.showsVerticalScrollIndicator = NO; [self.view addSubview:_collectionView3]; [_collectionView3 registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:collectionViewCell];}

2、實現具體代理方法 UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.modelArray.count;}- (NSMutableArray *)modelArray { if (!_modelArray) { _modelArray = [NSMutableArray array]; } return _modelArray;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CollModel *infoModel = self.modelArray[indexPath.row]; NSLog(@'section:%ld --- row:%ld -----%@',indexPath.section,indexPath.row,infoModel.title); CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionViewCell forIndexPath:indexPath]; cell.itemModel = 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 = self.modelArray[indexPath.row]; NSLog(@'infoModelArray----%@',infoModel.title);}

3、自定義UICollectionViewFlowLayout

LHLeftCollocationView.m 實現

#import 'LHLeftCollocationView.h'@implementation LHLeftCollocationView- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity { CGRect targectRect = CGRectMake(proposedContentOffset.x, 0.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); NSArray * attriArray = [super layoutAttributesForElementsInRect:targectRect]; CGFloat horizontalCenterX = proposedContentOffset.x + ([UIScreen mainScreen].bounds.size.width); CGFloat offsetAdjustment = CGFLOAT_MAX; for (UICollectionViewLayoutAttributes * layoutAttributes in attriArray) { CGFloat itemHorizontalCenterX = layoutAttributes.center.x; if (fabs(itemHorizontalCenterX-horizontalCenterX) < fabs(offsetAdjustment)) { offsetAdjustment = itemHorizontalCenterX - horizontalCenterX; } } return CGPointMake(proposedContentOffset.x , proposedContentOffset.y);}CGFloat ActiveDistance = 400; //垂直縮放除以系數CGFloat ScaleFactor = 0.50; //縮放系數 越大縮放越大- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect { NSArray * array = [super layoutAttributesForElementsInRect:rect]; CGRect visibleRect = CGRectZero; visibleRect.origin = self.collectionView.contentOffset; visibleRect.size = self.collectionView.bounds.size; for (UICollectionViewLayoutAttributes *attributes in array) { CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x; CGFloat normalizedDistance = fabs(distance / ActiveDistance); CGFloat zoom = 1 - ScaleFactor * normalizedDistance; NSLog(@'zoom----%f',zoom); attributes.transform3D = CATransform3DMakeScale(1.0, zoom, 1.0); //底部顯示效果 attributes.frame = CGRectMake(attributes.frame.origin.x, attributes.frame.origin.y + zoom, attributes.size.width, attributes.size.height); //居中顯示效果// CGFloat scrollDirectionItemHeight = self.itemSize.height;// CGFloat sideItemFixedOffset = 0;// sideItemFixedOffset = (scrollDirectionItemHeight - scrollDirectionItemHeight * 0.7) / 2;// attributes.center = CGPointMake(attributes.center.x, attributes.center.y + zoom); } return array;}////設置放大動畫//-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect//{// NSArray *arr = [self getCopyOfAttributes:[super layoutAttributesForElementsInRect:rect]];// //屏幕中線// CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.bounds.size.width/2.0f;// //刷新cell縮放// for (UICollectionViewLayoutAttributes *attributes in arr) {// CGFloat distance = fabs(attributes.center.x - centerX);// //移動的距離和屏幕寬度的的比例// CGFloat apartScale = distance/self.collectionView.bounds.size.width;// //把卡片移動范圍固定到 -π/4到 +π/4這一個范圍內// CGFloat scale = fabs(cos(apartScale * M_PI/4));// //設置cell的縮放 按照余弦函數曲線 越居中越趨近于1// attributes.transform = CGAffineTransformMakeScale(1.0, scale);// }// return arr;//}//防止報錯 先復制attributes- (NSArray *)getCopyOfAttributes:(NSArray *)attributes{ NSMutableArray *copyArr = [NSMutableArray new]; for (UICollectionViewLayoutAttributes *attribute in attributes) { [copyArr addObject:[attribute copy]]; } return copyArr;}- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return true;}@end

4、自定義cell 和model

model

#import <Foundation/Foundation.h>@interface CollModel : NSObject@property (nonatomic,strong)NSString *imgUrl;@property (nonatomic,strong)NSString *title;@property (nonatomic,strong)NSString *url;@end

cell 自定義

#import <UIKit/UIKit.h>#import 'CollModel.h'@interface CollectionViewCell : UICollectionViewCell@property (nonatomic, strong) CollModel * itemModel;@end#import 'CollectionViewCell.h'#define SCREEN_RATE ([UIScreen mainScreen].bounds.size.width/375.0)@interface CollectionViewCell()/** * 存放所有下載操作的隊列 */@property (nonatomic, strong) UIImageView *itemIcon;@property (nonatomic, strong) UILabel *itemLabel;@property (nonatomic, strong) UILabel *priceLabel;@end@implementation CollectionViewCell@synthesize itemModel = _itemModel;- (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.contentView.backgroundColor = [UIColor clearColor]; [self initView]; } return self;}- (void)initView { _itemIcon = [[UIImageView alloc] init]; [self.contentView addSubview:_itemIcon]; _itemIcon.backgroundColor = [UIColor clearColor]; // CGFloat iconWidth = ([UIScreen mainScreen].bounds.size.width / 5.0) * SCREEN_RATE; _itemIcon.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height); _itemIcon.center = self.contentView.center;}- (CollModel *)itemModel{ return _itemModel;}- (void)setItemModel:(CollModel *)itemModel{ if (!itemModel) { return; } _itemModel = itemModel; [self setCellWithModel:_itemModel];}- (void)setCellWithModel:(CollModel *)itemModel{ [[NSOperationQueue mainQueue] addOperationWithBlock:^{ _itemIcon.image = [UIImage imageNamed:itemModel.url]; }];}@end

運行效果

iOS UICollectionView實現卡片效果

下載demo

github 下載

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: IOS
相關文章:
主站蜘蛛池模板: 久久99精品这里精品3 | 欧美视频在线一区 | 中文字幕在线观看亚洲日韩 | 美女视频很黄很a免费国产 美女视频黄.免费网址 | 免费一级特黄a | 午夜香蕉成视频人网站高清版 | 亚洲综合视频网 | 久久精品一区二区免费看 | 国产欧美久久精品 | 久久99精品视香蕉蕉 | 欧美大片在线观看成人 | 中文字幕免费在线视频 | 黄色a三级三级三级免费看 黄色a网 | 久久com | 日本在线加勒比 | 亚洲精品欧美日韩 | 涩涩网站 | 国产欧美日韩精品第一区 | 日韩精品视频免费在线观看 | 97久久精品国产精品青草 | 免费亚洲成人 | 日韩欧美精品综合一区二区三区 | 玖玖视频精品 | 国产a国产 | 亚洲 欧美 丝袜 | 久久久久成人精品一区二区 | 日韩在线视精品在亚洲 | 欧美午夜性春猛交 | 天天都色| 国产情侣无套精品视频 | 日本特级淫片免费看 | 亚洲成人h| 国产亚洲精品久久久久久久久激情 | 国产二区自拍 | 久草资源在线播放 | 久久在线国产 | 香蕉久久久久久狠狠色 | 欧美视频一区在线观看 | 67194成人在线观看 | 精品国产成人a区在线观看 精品国产成人a在线观看 | 扒开双腿猛进入爽爽在线观看 |