前言
一般地,第一次打開APP時都會有幾個引導頁,提示用戶如何使用APP等。下面來簡單實現這樣的引導頁功能吧。
做法思路
可以用一個scrollview其中包含幾個imageview,或者直接用一個collectionView, 其cell就是一個imageview。這里采用第二種方法做。
自定義cell
新建一個類PDupdateLeadCell,繼承自UICollectionViewCell。先聲明cell的子控件,這里只需一個imageview。并實現懶加載
@interface PDupdateLeadCell()
@property(nonatomic,weak)UIImageView *imgView; //子控件
@end
@implementation PDupdateLeadCell
//懶加載
-(UIImageView *)imgView{
if(!_imgView){
self.backgroundColor = Color_alpha; //透明色
self.contentView.backgroundColor = Color_alpha;
UIImageView *imgV = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
if(screenH == 812) //適配iPhoneX
imgV.contentMode = UIViewContentModeScaleToFill;
[self.contentView addSubview:imgV];
_imgView = imgV;
}
return _imgView;
}
@end
再聲明一個圖片屬性作為cell的數據,以便外界可以傳進一張圖片
@property(nonatomic,strong)UIImage *backImage;
并在.m文件中,重寫set方法,
-(void)setBackImage:(UIImage *)backImage{
_backImage = backImage;
self.imgView.image = backImage;
}
到此,自定義的cell就完成了。下面來設計它的View控件。
引導頁的view
新建一個類PDleadingView繼承自UIView,先添加其子控件
@interface PDleadingView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,weak)UICollectionView *leadColltView;
@end
定義引導頁的頁數
static NSInteger leadPageNum = 5;
由于使用的是collectionView,所以需要創建流水布局,在創建collectionView。
-(void)addLayoutAndCollectionView{
//1、創建流水布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(screenW, screenH); // 設置每一個item的大小
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 0;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; //設置滾動方向-水平
//2、創建collecView
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH) collectionViewLayout:layout];
collectionView.backgroundColor = Color_alpha;
collectionView.showsHorizontalScrollIndicator = NO;
collectionView.scrollEnabled = NO; //關閉滑動
collectionView.bounces = NO;
//3、添加
[self addSubview:collectionView];
_leadColltView = collectionView;
}
在init方法中創建,添加。
-(instancetype)initWithFrame:(CGRect)frame{
if(self = [super initWithFrame:frame]){
self.backgroundColor = Color_alpha; //透明
[self addLayoutAndCollectionView];
// 注冊自定義的cell
[self.leadColltView registerClass:[PDupdateLeadCell class] forCellWithReuseIdentifier:@"leadcell"];
self.leadColltView.delegate = self; //別忘了設置代理,遵守協議
self.leadColltView.dataSource = self;
}
return self;
}
下面,實現collectionView的數據源方法
// 這里只需section數為1,所以沒有實現numberOfSectionsInCollectionView方法。
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return leadPageNum; //引導頁的頁數
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// 創建cell
PDupdateLeadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"leadcell" forIndexPath:indexPath];
// 給cell添加圖片,注意圖片的命名
NSString *name = [NSString stringWithFormat:@"page%ld",indexPath.item + 1];
UIImage *img = [UIImage imageNamed:name];
cell.backImage = img;
return cell;
}
下面設計cell的處理,需要實現的業務邏輯是,點一下,切換到下一張引導頁。
在以下這個代理方法中
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[collectionView deselectItemAtIndexPath:indexPath animated:YES];//取消選中
// 關鍵處理:通過改變collectionView的水平滑動距離ContentOffset,實現引導頁的切換
if(indexPath.row < leadPageNum - 1){
NSInteger page = indexPath.row + 1;
[self.leadColltView setContentOffset:CGPointMake(page * screenW, 0) animated:YES];
}
if(indexPath.row == leadPageNum - 1)
[self removeFromSuperview]; // 如果當前為最后一頁,點擊之后移除引導頁
}
到此,引導頁組件也設計好了,下面看外面怎么使用它吧。
在需要引導頁的控制器中,
#pragma mark -添加更新引導頁
-(void)addUpdateGuide{
// 拿到當前工程的Bundle id
NSString *cur_build = [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleVersion"];
//從本地存儲中,取出上一次版本
NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
NSString *last_build = [userD objectForKey:@"Bundle"];
//判斷當前版本號
if(last_build.length == 0 || last_build == nil){ //[cur_build integerValue] > [last_build integerValue]
//保存當前版本
[userD setObject:cur_build forKey:@"Bundle"];
[userD synchronize];
// 創建添加,顯示引導頁
PDleadingView *leadv = [[PDleadingView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
[self.view addSubview:leadv];
}
}