前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS 引导页实现方式

iOS 引导页实现方式

作者头像
码客说
发布2019-10-22 16:56:06
1.3K0
发布2019-10-22 16:56:06
举报
文章被收录于专栏:码客码客

简述

主要利用UICollectionViewUIPageControl配合来实现

代码

添加代理

代码语言:javascript
复制
UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

配置

代码语言:javascript
复制
@IBOutlet weak var collectionView: UICollectionView!
    
@IBOutlet weak var pageControl: UIPageControl!
    
var colletcionData:[String] = ["yindao1","yindao2","yindao3","yindao4"];
    
override func viewDidLoad() {
    super.viewDidLoad()

    self.initCollectionView();
    self.initPageControl();
}
    
func initCollectionView(){
    self.collectionView.register(UINib.init(nibName: "WelcomeCell", bundle: nil), forCellWithReuseIdentifier: "WelcomeCell");
    self.collectionView.showsHorizontalScrollIndicator = false;
    self.collectionView.showsVerticalScrollIndicator = false;
    self.collectionView.backgroundColor = UIColor.clear;
    self.collectionView.isScrollEnabled = true;
    
    let flowLayout = UICollectionViewFlowLayout();
    flowLayout.scrollDirection = UICollectionViewScrollDirection.horizontal;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.isPagingEnabled = true;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
}
    
func initPageControl(){
    self.pageControl.numberOfPages = self.colletcionData.count;
    self.pageControl.currentPage = 0;
    self.pageControl.setValue(UIImage.init(named: "pageControl1"), forKey: "_pageImage")
    self.pageControl.setValue(UIImage.init(named: "pageControl2"), forKey: "_currentPageImage")
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1;
}
    
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return colletcionData.count;
}
    
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let itemdata = colletcionData[indexPath.row];
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WelcomeCell", for: indexPath) as! WelcomeCell;
    cell.inImageView.image = UIImage.init(named: itemdata)
    return  cell;
}
    
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: self.collectionView.frame.width, height: self.collectionView.frame.height);
}
    
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    //点击最后一张图 跳转到登录页面
    if(indexPath.row == (self.colletcionData.count-1)){
        ZJUserDefaults.setValue("false", forKey: "isFirstLoad");
        let loginViewController = LoginViewController();
        AppDelegate.appDelegate?.window?.rootViewController = loginViewController;
    }
}
    
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let pageWidth = self.collectionView.frame.width;
    let currPage = Int(floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1);
    self.pageControl.currentPage = currPage;
    if(currPage == self.colletcionData.count - 1){
        self.pageControl.isHidden = true;
    }else{
        self.pageControl.isHidden = false;
    }
}

用到的Cell

代码语言:javascript
复制
import UIKit

class WelcomeCell: UICollectionViewCell {
    @IBOutlet weak var inImageView: UIImageView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
}

设置状态栏

代码语言:javascript
复制
//设置状态栏为白色
override var preferredStatusBarStyle: UIStatusBarStyle{
    return UIStatusBarStyle.lightContent;
}
    
override var prefersStatusBarHidden: Bool{
    return false;
}
    
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
    return UIStatusBarAnimation.slide
}

获取View的所有属性

主要用在修改一些组件的默认样式 这里用于获取UIPageControl的属性 修改原来的点为自己的图片

代码语言:javascript
复制
func runtime() {
    // 利用runtime 遍历出pageControl的所有属性
    var count : UInt32 = 0
    let ivars = class_copyIvarList(UIPageControl.self, &count)
    for i in 0..<count {
        let ivar = ivars?[Int(i)]
        let name = ivar_getName(ivar!)
        print(String(cString: name!))
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-12-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简述
  • 代码
  • 设置状态栏
  • 获取View的所有属性
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档