** 轉(zhuǎn)載請(qǐng)注明出處:http://www.reibang.com/p/024dd2d6e6e6#**
已更新至 Xcode8.2耳奕、Swift3
在第一次打開App或者App更新后通常用引導(dǎo)頁來展示產(chǎn)品特性
我們用NSUserDefaults
類來判斷程序是不是第一次啟動(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 說明是第一次啟動(dòng);如果 appVersion 不等于 currentAppVersion 說明是更新了
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
來裝載我們的引導(dǎo)頁:
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)
// 給開始按鈕設(shè)置圓角
startButton.layer.cornerRadius = 15.0
// 隱藏開始按鈕
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開始榜配,所以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