iOS UICollectionView實(shí)現(xiàn)卡片效果
現(xiàn)在使用卡片效果的app很多,之前公司讓實(shí)現(xiàn)一種卡片效果,就寫了一篇關(guān)于實(shí)現(xiàn)卡片的文章。文章最后附有demo
實(shí)現(xiàn)上我選擇了使用UICollectionView ;用UICollectionViewFlowLayout來定制樣式;下面看看具體實(shí)現(xiàn)
具體實(shí)現(xiàn)
1、創(chuàng)建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、實(shí)現(xiàn)具體代理方法 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;
}
// 返回每個(gè)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點(diǎn)擊事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
CollModel *infoModel = self.modelArray[indexPath.row];
NSLog(@"infoModelArray----%@",infoModel.title);
}
3、自定義UICollectionViewFlowLayout
LHLeftCollocationView.m 實(shí)現(xiàn)
#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; //垂直縮放除以系數(shù)
CGFloat ScaleFactor = 0.50; //縮放系數(shù) 越大縮放越大
- (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;
}
////設(shè)置放大動(dòng)畫
//-(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);
// //移動(dòng)的距離和屏幕寬度的的比例
// CGFloat apartScale = distance/self.collectionView.bounds.size.width;
// //把卡片移動(dòng)范圍固定到 -π/4到 +π/4這一個(gè)范圍內(nèi)
// CGFloat scale = fabs(cos(apartScale * M_PI/4));
// //設(shè)置cell的縮放 按照余弦函數(shù)曲線 越居中越趨近于1
// attributes.transform = CGAffineTransformMakeScale(1.0, scale);
// }
// return arr;
//}
//防止報(bào)錯(cuò) 先復(fù)制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()
/**
* 存放所有下載操作的隊(duì)列
*/
@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
運(yùn)行效果

下載demo
github 下載
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS實(shí)現(xiàn)類似微信和支付寶的密碼輸入框(UIKeyInput協(xié)議)
這篇文章主要介紹了iOS實(shí)現(xiàn)類似微信和支付寶的密碼輸入框,通過UIKeyInput協(xié)議為響應(yīng)者提供簡單的鍵盤輸入的功,再通過CoreGraphics繪制出密碼輸入框,感興趣的小伙伴們可以參考一下2016-08-08
詳解IOS開發(fā)之實(shí)現(xiàn)App消息推送(最新)
這篇文章主要介紹了詳解IOS開發(fā)之實(shí)現(xiàn)App消息推送(最新),具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12
iOS多控制器實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了iOS多控制器實(shí)現(xiàn)帶滑動(dòng)動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
iOS11 下載之?dāng)帱c(diǎn)續(xù)傳的bug的解決方法
本篇文章主要介紹了iOS11 下載之?dāng)帱c(diǎn)續(xù)傳的bug的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
iOS利用CoreImage實(shí)現(xiàn)人臉識(shí)別詳解
OS的人臉識(shí)別從iOS 5(2011)就有了,不過一直沒怎么被關(guān)注過。人臉識(shí)別API允許開發(fā)者不僅可以檢測(cè)人臉,也可以檢測(cè)到面部的一些特殊屬性,比如說微笑或眨眼。下面這篇文章主要給大家介紹了iOS利用CoreImage實(shí)現(xiàn)人臉識(shí)別的相關(guān)資料,需要的朋友可以參考下。2017-05-05
IOS中MMDrawerController第三方抽屜效果的基本使用示例
這篇文章主要介紹了IOS中MMDrawerController第三方抽屜效果的基本使用示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
iOS開發(fā)Masonry與Frame布局差異示例詳解
這篇文章主要為大家介紹了iOS開發(fā)Masonry與Frame布局差異示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

