とりあえず、BWWalkthroughをDL
ビルドするとこんな感じの動き
ちょっとだけ解説
まず、Storyboardが二つ用意されている。
- Main.storyboard
- Walkthrough.storyboard
起動すると、すぐにWalkthrough.storyboardのものが表示されているが
ViewController.swift
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let userDefaults = NSUserDefaults.standardUserDefaults()
if !userDefaults.boolForKey("walkthroughPresented") {
showWalkthrough()
userDefaults.setBool(true, forKey: "walkthroughPresented")
userDefaults.synchronize()
}
}
userDefaults.boolForKey("walkthroughPresented")がtrueでなかった場合
showWalkthrough()メソッドを読みにいっている
ViewController.swift
@IBAction func showWalkthrough(){
// Get view controllers and build the walkthrough
let stb = UIStoryboard(name: "Walkthrough", bundle: nil)
let walkthrough = stb.instantiateViewControllerWithIdentifier("walk") as! BWWalkthroughViewController
let page_zero = stb.instantiateViewControllerWithIdentifier("walk0") as! UIViewController
let page_one = stb.instantiateViewControllerWithIdentifier("walk1") as! UIViewController
let page_two = stb.instantiateViewControllerWithIdentifier("walk2")as! UIViewController
let page_three = stb.instantiateViewControllerWithIdentifier("walk3") as! UIViewController
// Attach the pages to the master
walkthrough.delegate = self
walkthrough.addViewController(page_one)
walkthrough.addViewController(page_two)
walkthrough.addViewController(page_three)
walkthrough.addViewController(page_zero)
self.presentViewController(walkthrough, animated: true, completion: nil)
}
showWalkthrough()では、Storyboardを指定してページング用のページに各Viewを割り当てたのちに表示している。
Walkthroughで表示されるViewにある各種ボタンの処理は、BWWalkthroughViewController内に記述されている。
BWWalkthroughViewController.swift
@IBAction func nextPage(){
if (currentPage + 1) < controllers.count {
delegate?.walkthroughNextButtonPressed?()
var frame = scrollview.frame
frame.origin.x = CGFloat(currentPage + 1) * frame.size.width
scrollview.scrollRectToVisible(frame, animated: true)
}
}
@IBAction func prevPage(){
if currentPage > 0 {
delegate?.walkthroughPrevButtonPressed?()
var frame = scrollview.frame
frame.origin.x = CGFloat(currentPage - 1) * frame.size.width
scrollview.scrollRectToVisible(frame, animated: true)
}
}
@IBAction func close(sender: AnyObject){
delegate?.walkthroughCloseButtonPressed?()
}
とくに、動きを変更しないのであれば、Walkthrough.storyboardのViewにある画像やテキストを変えるだけで対応できる。Viewを増やして、ViewController.swiftのshowWalkthrough()メソッドで指定してあげれば対応できる。
userDefaultsを使っているので、二回目以降は起動時に説明文がでなくなる。