引导页面的实现,无非就是UIScrollerView,第三方库,自己封装的VC。用户体验讲少即是美,其实有时候我们真的没必要把引导页搞得花里胡哨的。简单简洁就好。而UICollectionVIewController没有比它更简单的了。
项目github下载地址
CollectionViewController
无需多说直接上代码:
//
// CollectionViewController.m
// UICollectionVIewDemo
//
// Created by DaLei on 2017/6/7.
// Copyright © 2017年 DaLei. All rights reserved.
//
#import "CollectionViewController.h"
#import "CollectionViewCell.h"
@interface CollectionViewController ()
@property(nonatomic,strong)NSArray*arrayList;
@end
@implementation CollectionViewController
static NSString * const reuseIdentifier = @"CollectionViewCell";
-(instancetype)init{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//引导页面大小和屏幕一致
layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height + 20);
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 0;
//横向滑动
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
return [self initWithCollectionViewLayout:layout];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"首页";
self.navigationController.navigationBarHidden = YES;
// Register cell classes
[self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
// 开启分页
self.collectionView.pagingEnabled = YES;
// 隐藏水平滚动条
self.collectionView.showsHorizontalScrollIndicator = NO;
// 取消弹簧效果
self.collectionView.bounces = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark <UICollectionViewDataSource>
-(NSArray *)arrayList{
if (!_arrayList) {
_arrayList = @[@"appIntro1",@"appIntro2",@"appIntro3"];
}
return _arrayList;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.arrayList.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
//设置Cell显示的图片
[cell.iamgeVC setImage:[UIImage imageNamed:[self.arrayList objectAtIndex:indexPath.item]]];
//判断在尾页的时候显示出“启动”Button
if (indexPath.item == self.arrayList.count - 1) {
cell.buttonStart.hidden = NO;
} else {
cell.buttonStart.hidden = YES;
}
return cell;
}
#pragma mark <UICollectionViewDelegate>
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"------%zd", indexPath.section);
NSLog(@"------%zd", indexPath.item);
}
@end
CollectionViewCell
先定义两个空控件,一个图片和一个按钮。
在XIb中拖入两个控件,记得要通过拉线的方式和代码中的IBOutlet关联,然后自通过拉线的方式定义约束。
运行效果:
学习参考:Developer_CYX
UICollectionViewController基础(1)
UICollectionViewController基础(2)引导页
UICollectionViewController基础(3)瀑布流
UICollectionViewController基础(4)自定义