[iOS] App 引導(dǎo)頁(yè)的簡(jiǎn)單實(shí)現(xiàn) (Swift 3)

** 轉(zhuǎn)載請(qǐng)注明出處:http://www.itdecent.cn/p/024dd2d6e6e6#**

已更新至 Xcode8.2、Swift3

在第一次打開(kāi)App或者App更新后通常用引導(dǎo)頁(yè)來(lái)展示產(chǎn)品特性

我們用NSUserDefaults類來(lái)判斷程序是不是第一次啟動(dòng)或是否更新,在 AppDelegate.swift中加入以下代碼:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // 得到當(dāng)前應(yīng)用的版本號(hào)
    let infoDictionary = Bundle.main.infoDictionary
    let currentAppVersion = infoDictionary!["CFBundleShortVersionString"] as! String
        
    // 取出之前保存的版本號(hào)
    let userDefaults = UserDefaults.standard
    let appVersion = userDefaults.string(forKey: "appVersion")
        
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
        
    // 如果 appVersion 為 nil 說(shuō)明是第一次啟動(dòng);如果 appVersion 不等于 currentAppVersion 說(shuō)明是更新了
    if appVersion == nil || appVersion != currentAppVersion {
        // 保存最新的版本號(hào)
        userDefaults.setValue(currentAppVersion, forKey: "appVersion")
            
        let guideViewController = storyboard.instantiateViewController(withIdentifier: "GuideViewController") as! GuideViewController
        self.window?.rootViewController = guideViewController
    }
        
    return true
}

GuideViewController中,我們用UIScrollView來(lái)裝載我們的引導(dǎo)頁(yè):

class GuideViewController: UIViewController {
    
    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var startButton: UIButton!
    
    fileprivate var scrollView: UIScrollView!
    
    fileprivate let numOfPages = 3

    override func viewDidLoad() {
        super.viewDidLoad()

        let frame = self.view.bounds
        
        scrollView = UIScrollView(frame: frame)
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        scrollView.contentOffset = CGPoint.zero
        // 將 scrollView 的 contentSize 設(shè)為屏幕寬度的3倍(根據(jù)實(shí)際情況改變)
        scrollView.contentSize = CGSize(width: frame.size.width * CGFloat(numOfPages), height: frame.size.height)
        
        scrollView.delegate = self
        
        for index  in 0..<numOfPages {
            let imageView = UIImageView(image: UIImage(named: "GuideImage\(index + 1)"))
            imageView.frame = CGRect(x: frame.size.width * CGFloat(index), y: 0, width: frame.size.width, height: frame.size.height)
            scrollView.addSubview(imageView)
        }
        
        self.view.insertSubview(scrollView, at: 0)
        
        // 給開(kāi)始按鈕設(shè)置圓角
        startButton.layer.cornerRadius = 15.0
        // 隱藏開(kāi)始按鈕
        startButton.alpha = 0.0
    }
    
    // 隱藏狀態(tài)欄
    override var prefersStatusBarHidden : Bool {
        return true
    }
}

最后我們讓GuideViewController遵循UIScrollViewDelegate協(xié)議,在這里判斷是否滑動(dòng)到最后一張以顯示進(jìn)入按鈕:

// MARK: - UIScrollViewDelegate
extension GuideViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        // 隨著滑動(dòng)改變pageControl的狀態(tài)
        pageControl.currentPage = Int(offset.x / view.bounds.width)
        
        // 因?yàn)閏urrentPage是從0開(kāi)始,所以numOfPages減1
        if pageControl.currentPage == numOfPages - 1 {
            UIView.animate(withDuration: 0.5, animations: {
                self.startButton.alpha = 1.0
            }) 
        } else {
            UIView.animate(withDuration: 0.2, animations: {
                self.startButton.alpha = 0.0
            }) 
        }
    }
}

在上面的代碼中,為了顯得自然我們給進(jìn)入按鈕加入了一點(diǎn)動(dòng)畫 :]

最終效果如下:


GuideScreenshot.gif

Github地址:https://github.com/GuiminChu/JianshuExample

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容