最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS UICollectionView實現(xiàn)卡片效果

 更新時間:2020年04月21日 15:12:23   作者:哈皮吖  
這篇文章主要為大家詳細介紹了iOS UICollectionView實現(xiàn)卡片效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

具體實現(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、實現(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;
}

// 返回每個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 實現(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;
}

////設置放大動畫
//-(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這一個范圍內(nèi)
// CGFloat scale = fabs(cos(apartScale * M_PI/4));
// //設置cell的縮放 按照余弦函數(shù)曲線 越居中越趨近于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

運行效果

下載demo

github 下載

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • iOS開發(fā)中WebView的基本使用方法簡介

    iOS開發(fā)中WebView的基本使用方法簡介

    這篇文章主要介紹了iOS開發(fā)中WebView的基本使用方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-11-11
  • iOS開發(fā)TableView網(wǎng)絡請求及展示預加載實現(xiàn)示例

    iOS開發(fā)TableView網(wǎng)絡請求及展示預加載實現(xiàn)示例

    這篇文章主要為大家介紹了iOS開發(fā)TableView網(wǎng)絡請求及展示預加載實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • iOS中關于Swift UICollectionView橫向分頁的問題

    iOS中關于Swift UICollectionView橫向分頁的問題

    這篇文章通過圖文并茂的形式給大家介紹UICollectionView橫向分頁的問題,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧
    2017-05-05
  • iOS-Mac配置Tomcat教程 Mac環(huán)境配置Tomcat教程

    iOS-Mac配置Tomcat教程 Mac環(huán)境配置Tomcat教程

    這篇文章主要介紹了iOS-Mac配置Tomcat教程,Mac環(huán)境配置Tomcat,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • iOS中NSObject的兩種含義:類和協(xié)議詳解

    iOS中NSObject的兩種含義:類和協(xié)議詳解

    這篇文章主要給大家介紹了關于iOS中NSObject的兩種含義:類和協(xié)議的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-09-09
  • iOS利用UITableView設置全屏分隔線的3種方法總結

    iOS利用UITableView設置全屏分隔線的3種方法總結

    這篇文章主要介紹了關于iOS利用UITableView設置全屏分隔線的幾種方法的相關對比,分析這三種的各自優(yōu)缺點,并且分享了設置UITableView的單元格分割線離屏幕左右的距離為0的方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起看看吧。
    2017-11-11
  • iOS下border-image不起作用的解決辦法

    iOS下border-image不起作用的解決辦法

    上周五突然接到現(xiàn)場的一個需求,做一個移動端的勞模展示頁面。下面小編給大家介紹下iOS下border-image不起作用的解決辦法,需要的朋友參考下吧
    2017-05-05
  • iOS圖片拉伸的方法

    iOS圖片拉伸的方法

    這篇文章主要為大家詳細介紹了iOS圖片拉伸的相關方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • iOS獲取短信驗證碼倒計時的兩種實現(xiàn)方法

    iOS獲取短信驗證碼倒計時的兩種實現(xiàn)方法

    本篇文章主要介紹了iOS獲取短信驗證碼倒計時的兩種實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • 詳解IOS四種保存數(shù)據(jù)的方式

    詳解IOS四種保存數(shù)據(jù)的方式

    本篇文章主要介紹了OS四種保存數(shù)據(jù)的方式,現(xiàn)在分享給大家,也給大家做個參考。感興趣的小伙伴們可以參考一下。
    2016-11-11

最新評論

轮台县| 宁海县| 和政县| 梁平县| 芜湖县| 锡林浩特市| 汝城县| 黑水县| 松阳县| 远安县| 偏关县| 沂源县| 祁东县| 嘉义县| 武汉市| 田东县| 大同市| 永嘉县| 绥芬河市| 聊城市| 托克逊县| 鱼台县| 华容县| 米林县| 侯马市| 米脂县| 寻甸| 博乐市| 阆中市| 富川| 泰来县| 赞皇县| 阿拉善左旗| 行唐县| 洛川县| 徐水县| 大足县| 玛沁县| 松溪县| 嘉义县| 安远县|