App引導頁的簡單實現——iOS(Swift 2)

HermineHawk 8年前發布 | 8K 次閱讀 Swift iOS開發 移動開發

來自: http://finalshares.com/read-7027

已更新至 Xcode7.2、Swift2.1

在第一次打開App或者App更新后通常用引導頁來展示產品特性

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

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

// 得到當前應用的版本號
let infoDictionary = NSBundle.mainBundle().infoDictionary
let currentAppVersion = infoDictionary!["CFBundleShortVersionString"] as! String

// 取出之前保存的版本號
let userDefaults = NSUserDefaults.standardUserDefaults()
let appVersion = userDefaults.stringForKey("appVersion")

let storyboard = UIStoryboard(name: "Main", bundle: nil)

// 如果 appVersion 為 nil 說明是第一次啟動;如果 appVersion 不等于 currentAppVersion 說明是更新了
if appVersion == nil || appVersion != currentAppVersion {
    // 保存最新的版本號
    userDefaults.setValue(currentAppVersion, forKey: "appVersion")

    let guideViewController = storyboard.instantiateViewControllerWithIdentifier("GuideViewController") as! GuideViewController
    self.window?.rootViewController = guideViewController
}

return true

}</pre>

GuideViewController 中,我們用 UIScrollView 來裝載我們的引導頁:

class GuideViewController: UIViewController {

@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var startButton: UIButton!

private var scrollView: UIScrollView!

private let numOfPages = 3

override func viewDidLoad() {
    super.viewDidLoad()

    let frame = self.view.bounds

    scrollView = UIScrollView(frame: frame)
    scrollView.pagingEnabled = true
    scrollView.showsHorizontalScrollIndicator = false
    scrollView.showsVerticalScrollIndicator = false
    scrollView.scrollsToTop = false
    scrollView.bounces = false
    scrollView.contentOffset = CGPointZero
    // 將 scrollView 的 contentSize 設為屏幕寬度的3倍(根據實際情況改變)
    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, atIndex: 0)

    // 給開始按鈕設置圓角
    startButton.layer.cornerRadius = 15.0
    // 隱藏開始按鈕
    startButton.alpha = 0.0
}

// 隱藏狀態欄
override func prefersStatusBarHidden() -> Bool {
    return true
}

}</pre>

最后我們讓 GuideViewController 遵循 UIScrollViewDelegate 協議,在這里判斷是否滑動到最后一張以顯示進入按鈕:

// MARK: - UIScrollViewDelegate
extension GuideViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        // 隨著滑動改變pageControl的狀態
        pageControl.currentPage = Int(offset.x / view.bounds.width)

    // 因為currentPage是從0開始,所以numOfPages減1
    if pageControl.currentPage == numOfPages - 1 {
        UIView.animateWithDuration(0.5) {
            self.startButton.alpha = 1.0
        }
    } else {
        UIView.animateWithDuration(0.2) {
            self.startButton.alpha = 0.0
        }
    }
}

}</pre>

在上面的代碼中,為了顯得自然我們給進入按鈕加入了一點動畫 :]

最終效果如下:

圖片:179888-648bc09efcd64289.gif


源代碼戳這里

文章轉自:簡書( 原文 )                                           感謝作者: 老初

</div>

 本文由用戶 HermineHawk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!