• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift3.0项目引导页

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

项目引导页并不难,使用 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()
        
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift中@available和#available发布时间:2022-07-13
下一篇:
swift:谈谈swift几种常见属性的区别发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap