iOS使用UICollectionView实现横向滚动照片效果-创新互联
本文实例为大家分享了iOS使用UICollectionView实现横向滚动展示照片的具体代码,供大家参考,具体内容如下
创新互联公司是一家专注于成都网站建设、成都网站设计与策划设计,柳河网站建设哪家好?创新互联公司做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:柳河等地区。柳河做网站价格咨询:18980820575这是Demo链接
效果图

思路
1. 界面搭建
界面的搭建十分简单,采用UICollectionView和自定义cell进行搭建即可。
// ViewController.m
// 下面使用到的宏和全局变量
#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
static NSString *const cellID = @"cellID";
// 创建collectionView的代码
- (void)setupCollectionView
{
// 使用系统自带的流布局(继承自UICollectionViewLayout)
UICollectionViewFlowLayout *layout = ({
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// 每个cell的大小
layout.itemSize = CGSizeMake(180, 180);
// 横向滚动
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// cell间的间距
layout.minimumLineSpacing = 40;
//第一个cell和最后一个cell居中显示(这里我的Demo里忘记改了我用的是160,最后微调数据cell的大小是180)
CGFloat margin = (ScreenW - 180) * 0.5;
layout.sectionInset = UIEdgeInsetsMake(0, margin, 0, margin);
layout;
});
// 使用UICollectionView必须设置UICollectionViewLayout属性
UICollectionView *collectionView = ({
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
collectionView.center = self.view.center;
collectionView.bounds = CGRectMake(0, 0, ScreenW, 200);
collectionView.backgroundColor = [UIColor brownColor];
// 这里千万记得在interface哪里写!!!
collectionView.dataSource = self;
[collectionView setShowsHorizontalScrollIndicator:NO];
[self.view addSubview:collectionView];
collectionView;
});
// 实现注册cell,其中PhotoCell是我自定义的cell,继承自UICollectionViewCell
UINib *collectionNib = [UINib nibWithNibName:NSStringFromClass([PhotoCell class])
bundle:nil];
[collectionView registerNib:collectionNib
forCellWithReuseIdentifier:cellID];
}
// UICollectionViewCellDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return 10;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID
forIndexPath:indexPath];
// 图片名是 0 ~ 9
cell.imageName = [NSString stringWithFormat:@"%ld", (long)indexPath.row];
return cell;
} 另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网页标题:iOS使用UICollectionView实现横向滚动照片效果-创新互联
文章来源:http://www.scyingshan.cn/article/dhpdep.html


咨询
建站咨询
