LoginSignup
7
6

More than 3 years have passed since last update.

swift 簡単でおしゃれなチュートリアルライブラリ,Instructions

Last updated at Posted at 2020-04-19

使うライブラリ

Instructions
https://github.com/ephread/Instructions

tableViewみたいに直感的に使えてすごく便利です。

ViewController.swift
class ViewController: UIViewController{

//手取り早くstoryboardで設置して紐付けといてください。
    @IBOutlet weak var startButton: UIButton!
    let coachMarksController = CoachMarksController()
    //pointOfInterestが指し示されます。スポットライトが当たるみたいに
    private var pointOfInterest:UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.coachMarksController.dataSource = self
        self.pointOfInterest = self.startButton 

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated) 
        self.coachMarksController.start(in: .currentWindow(of: self))
    }
}

extension ViewController:CoachMarksControllerDataSource, CoachMarksControllerDelegate{
    func numberOfCoachMarks(for coachMarksController: CoachMarksController) -> Int {
//表示するスポットライトの数。チュートリアルの数。
        return 1
    }

    func coachMarksController(_ coachMarksController: CoachMarksController,
                                  coachMarkAt index: Int) -> CoachMark {
//指し示す場所を決める。 今回はpointOfInterestすなわちButtonga指し示される
        return coachMarksController.helper.makeCoachMark(for: pointOfInterest)
    }


//tableview でいうreturn cellに似てるのかなってイメージ。表示するチュートリアルメッセージなどがいじれる
    func coachMarksController(_ coachMarksController: CoachMarksController, coachMarkViewsAt index: Int, madeFrom coachMark: CoachMark) -> (bodyView: CoachMarkBodyView, arrowView: CoachMarkArrowView?) {
        let coachViews = coachMarksController.helper.makeDefaultCoachViews(withArrow: true, withNextText: true, arrowOrientation: coachMark.arrowOrientation)
        coachViews.bodyView.hintLabel.text = "チュートリアルメッセージです!"
        coachViews.bodyView.nextLabel.text = "OK"
        return (bodyView: coachViews.bodyView, arrowView: coachViews.arrowView)
    }
}

スクリーンショット 2020-04-19 22.08.16.png

注意すること

github上でのサンプルコードと異なる点

self.coachMarksController.start(in: .window(over: self))

本来ならこのコードで動くはずなのですが、ERRORに遭遇

[ERROR] The overlay view added to the window has empty bounds, Instructions will stop.

https://github.com/ephread/Instructions/issues/231
こちらで挙げられていました。下記のように修正したら無事動きました!

self.coachMarksController.start(in: .currentWindow(of: self))
7
6
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
7
6