iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
👨‍🏫

[Swift 5] Creating Easy App Tutorials with Instructions

に公開
2021/03/26

Introduction

When developing an app as an individual, I think it's quite a lot of work to create instructions for how to use the app from scratch.
By using Instructions, you can implement a tutorial relatively easily (you'll see what it looks like as you scroll down).

Library Information

Instructions

https://github.com/ephread/Instructions

Installation

Add the following to your Podfile:

pod 'Instructions', '~> 2.0.1'

Finished Image

Image borrowed from GitHub.

Implementation

In Instructions, tutorial-related elements are named with Coach (meaning like an instructor).

Import

Import Instructions in the ViewController where you want to display the tutorial.

HogeViewController.swift
import Instructions

Extend with an Extension

For now, just extend it; we'll add the content later.

HogeViewController.swift
//MARK: Instructions
extension HogeViewController: CoachMarksControllerDataSource, CoachMarksControllerDelegate {
    // We'll add various things here later
}
HogeViewController.swift
class HogeViewController {
	// Create a coach view controller
	let coachMarksController = CoachMarksController()
	
	// In viewDidLoad()
	override func viewDidLoad() {
	    super.viewDidLoad()
		
	    // Link the data source
	    self.coachMarksController.dataSource = self
	    // It will result in an error if you don't extend it with the extension first
	}
}

Decide the Number of Tutorial Steps

Decide the number of tutorial steps inside the extension we created earlier and return it.
Think of the number of steps as the number of speech bubbles that say something like "Press here for this | OK!".

HogeViewController.swift
// Add this inside the extension from earlier

/// Place to decide the number of speech bubbles
func numberOfCoachMarks(for coachMarksController: CoachMarksController) -> Int {		
	/// Change this to match the number of steps you want to set
	return 3
}

Decide the View to Spotlight

Decide the source View from which the speech bubble will appear.
This will also be added to the extension.

HogeViewController.swift
/**
    Returns the view that will be spotlighted
    - Parameters:
        - index: Which step it is
*/
func coachMarksController(_ coachMarksController: CoachMarksController,
                          coachMarkAt index: Int) -> CoachMark {
			  
    // Basically, I think it's best to put the buttons or views you want to target for the tutorial into an Array in the order of the tutorial and specify them by index (please let me know if there's a better way).
    let highlightViews: Array<UIView> = [hogeLabel, fugaButton, piyoSwtich]
    // (Setting the flow so hogeLabel is first, then fugaButton, and finally piyoSwitch)

    // Specify which step it is using the index from the views used in the tutorial
    return coachMarksController.helper.makeCoachMark(for: highlightViews[index])
}

2021/3/26 Update

When you want to spotlight an item in the navigation bar

As it is, it cannot be put into an Array, and it won't even work with coachMarksController.helper.makeCoachMark().
To spotlight a navigation item, do the following:

HogeViewController.swift
let highlightViews: Array<UIView> = [hogeLabel, 
				     fugaButton, 
				     piyoSwtich,
				     someNaviBarItem.value(forKey: "view") as! UIView]
				     // Get the item's view and add it

Deciding the Content of the Speech Bubble

Set the content of the speech bubble that appears when the view is spotlighted.
This will also be added to the extension.

HogeViewController.swift
/** 
    Decide the details of the speech bubble
    - Parameters: 
        - coachMarksController: VC for the tutorial
	- index: Which step it is
	- coachMark: Speech bubble
    - Returns: Speech bubble
*/
func coachMarksController(
    _ coachMarksController: CoachMarksController,
    coachMarkViewsAt index: Int,
    madeFrom coachMark: CoachMark
) -> (bodyView: UIView & CoachMarkBodyView, arrowView: (UIView & CoachMarkArrowView)?) {

    // Create the view for the speech bubble
    let coachViews = coachMarksController.helper.makeDefaultCoachViews(
        withArrow: true,    // Whether to attach a triangular arrow
        arrowOrientation: coachMark.arrowOrientation    // Orientation of the arrow (position of the speech bubble)
    )

    // Branch the display content based on the index (step)
    switch index {
    case 0:    // hogeLabel
        coachViews.bodyView.hintLabel.text = "This is hogeLabel"
        coachViews.bodyView.nextLabel.text = "OK!"
	
    case 1:    // fugaButton
	coachViews.bodyView.hintLabel.text = "Something good happens when you press this button"
        coachViews.bodyView.nextLabel.text = "👌"
    
    case 2:    // piyoSwitch
        coachViews.bodyView.hintLabel.text = "You can toggle settings with this switch"
        coachViews.bodyView.nextLabel.text = "Got it"
    
    default:
        break
	
    }
    
    // Return the speech bubble after other settings are finished
    return (bodyView: coachViews.bodyView, arrowView: coachViews.arrowView)
}

Starting the Tutorial

You can start the tutorial simply by executing the following line:

self.coachMarksController.start(in: .currentWindow(of: self))

In the GitHub sample, it was:

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

However, it did not work in my environment, so I recommend the former approach.

Usually, you'll want to start the tutorial as soon as the screen is loaded, so call it in viewDidAppear():

HogeViewController.swift
override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    // Start the tutorial!
    self.coachMarksController.start(in: .currentWindow(of: self))
}

Also, if you want to stop the tutorial halfway through, such as letting the user skip it, you can execute this:

self.coachMarksController.stop(immediately: true)

For details on fine-grained parameters and more, please refer to the GitHub page.

Wrap-up

It's intuitive and useful.
I felt it would be very handy for solo development.

Please let me know if there are any mistakes.

GitHubで編集を提案

Discussion