設計ios的版本更新引導頁

前言

一般地,第一次打開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];
    
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,245評論 4 61
  • 內容抽屜菜單ListViewWebViewSwitchButton按鈕點贊按鈕進度條TabLayout圖標下拉刷新...
    皇小弟閱讀 46,888評論 22 665
  • 原文鏈接:https://github.com/opendigg/awesome-github-android-u...
    IM魂影閱讀 32,965評論 6 472
  • 轉瞬間2017年度過了一大半了,回過頭來觀望年末計劃,會發現再嚴謹的計劃都會被現實的不可控性給擊潰到體無...
    執墨晚夏閱讀 267評論 0 2
  • 一宿沒睡。翻了一番云相冊 ,突然想把記憶找回來,雖然最不喜歡回過頭看生活,聊了一些發現,記憶比想象中的要深刻。
    卡卡卡塔爾閱讀 241評論 0 1