LoginSignup
12
13

More than 5 years have passed since last update.

BWWalkthroughを使ってみた

Last updated at Posted at 2015-04-29

とりあえず、BWWalkthroughDL

Move

ビルドするとこんな感じの動き

ちょっとだけ解説

まず、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.storyboardViewにある画像やテキストを変えるだけで対応できる。Viewを増やして、ViewController.swiftshowWalkthrough()メソッドで指定してあげれば対応できる。

userDefaultsを使っているので、二回目以降は起動時に説明文がでなくなる。

12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13