使うライブラリ
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)
}
}
注意すること
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))