swift3.0 项目引导页

Posted _水畔竹汐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift3.0 项目引导页相关的知识,希望对你有一定的参考价值。

项目引导页并不难,使用 UICollectionView就可以完成,

1.首先获取应用程序的版本号,并存入本地,每次有新版本号,和存入本地的版本号,相比较

 fileprivate func setupRootViewController() {
    
        window = UIWindow(frame: UIScreen.main.bounds)
        print(isNewVersion())
        window?.rootViewController = isNewVersion() ? NewFeatureController() : UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
        if isNewVersion() {
            //此处应该加载用户操作教程
        }else {
            //此处加载广告页面
            loadingAdvertisingPages()
        }
        window?.makeKeyAndVisible()
    }

/**
     判断是否是新版本
     */
    fileprivate func isNewVersion() -> Bool {
        // 获取当前的版本号

        let versionString = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        
        let currentVersion = Double(versionString)!
        
        // 获取到之前的版本号
        let versionKey = "curVersion"
        let sandboxVersion = UserDefaults.standard.double(forKey: versionKey)
        
        // 保存当前版本号
        UserDefaults.standard.set(currentVersion, forKey: versionKey)
        UserDefaults.standard.synchronize()
        
        // 对比
        return currentVersion > sandboxVersion
    }

新建一个继承  UICollectionViewController 的类,具体的代码如下

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureController: UICollectionViewController {
    fileprivate let itemCount = 4

    fileprivate var layout = UICollectionViewFlowLayout()
    //MARK: - 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView!.register(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.collectionView?.isPagingEnabled = true
        self.collectionView?.bounces = false
        self.collectionView?.showsHorizontalScrollIndicator = false
        self.collectionView?.backgroundColor = UIColor.white
        layout.scrollDirection = UICollectionViewScrollDirection.horizontal

        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = UIScreen.main.bounds.size
    }
    
    init() {
        super.init(collectionViewLayout:layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

// MARK: UICollectionViewDataSource
extension NewFeatureController {

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! NewFeatureCell
        
        cell.imageIndex = String(indexPath.item)
        cell.setIndexPath(indexPath: indexPath, count: itemCount)
        return cell

    }

}
import UIKit

class NewFeatureCell: UICollectionViewCell {
    
    //MARK: - 懒加载属性
    fileprivate lazy var imageView: UIImageView = UIImageView()
    
    fileprivate lazy var startButton: UIButton = UIButton()
    var  imageName: String?
    
    var imageIndex: String? {
    
        didSet {

            let screenH = UIScreen.main.bounds.height
            
            //加载不同的图片
            if screenH == 568  {
                imageName = imageIndex!+"welcome1136"
            }else if screenH == 667 {
                imageName = imageIndex!+"welcome1334"
            }else {
                imageName = imageIndex!+"welcome1472"
            }
            
            imageView.image = UIImage(named: imageName!)

        }
    }
        
    //MARK: - 构造函数
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: - 设置 UI界面相关
extension NewFeatureCell {

    fileprivate func setupUI() {
    
        startButton.setTitle("开始全新体验", for: .normal)
        startButton.setTitleColor(UIColor.orange, for: .normal)
        startButton.sizeToFit()
        startButton.isHidden = true
        startButton.addTarget(self, action: #selector(NewFeatureCell.startBtnClick), for: .touchUpInside)
        self.contentView .addSubview(imageView)
        self.contentView.addSubview(startButton)
        
    }
    
    //布局子控件的 frame
    override func layoutSubviews() {
        super.layoutSubviews()
        imageView.frame = self.bounds
        
        startButton.center = CGPoint(x: self.bounds.width * 0.5, y: self.bounds.height * 0.9)
    }
    
    func setIndexPath(indexPath: IndexPath, count: Int) {
        if indexPath.item == count-1 {
            startButton.isHidden = false
        }else {
            startButton.isHidden = true
        }
    }
    
}

//MARK: - 事件的点击
extension NewFeatureCell {

    @objc fileprivate func startBtnClick() {
        UIApplication.shared.keyWindow?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
        
    }
}

 

以上是关于swift3.0 项目引导页的主要内容,如果未能解决你的问题,请参考以下文章

项目重构-引导页的实现

微信小程序代码片段

react-navigation从引导页跳转到主页,物理回退键,不能再返回到引导页,代码怎么写?

Android 引导页公共方法LeaderPager

iOS 启动页和引导页的分割适配法

粘性页脚引导程序 4 [重复]