swiftでScrollViewを使ってチュートリアル画面を作る

コピペで動くように
xibを使わずにViewControllerだけで作りました。

スクロールさせると次の画像にスライドする
画面の下にページコントローラーもある
ボタンでもページを動くことができるものです

import UIKit

class FirstViewController: UIViewController, UIScrollViewDelegate {
    private var scrollView: UIScrollView? = nil
    private var button: UIButton? = nil
    private var pageControl: UIPageControl? = nil
    private var scrollViewFrame: CGRect = CGRect.zero
    private let images = [
        "a",
        "a",
        "a",
        "a",
        "a",
        ]
    private let color = [ UIColor.red, UIColor.blue, UIColor.yellow, UIColor.black, UIColor.cyan ]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.scrollViewFrame = UIScreen.main.bounds
        self.view.backgroundColor = UIColor.white
        
        // scroll view設定
        self.scrollView = self.makeScrollView()
        self.scrollView?.delegate = self
        self.view.addSubview(self.scrollView!)
        
        // page control設定
        self.pageControl = self.makePageControl()
        self.view.addSubview(self.pageControl!)
        
        // button設定
        self.button = self.makeNextButton()
        self.view.addSubview(self.button!)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    @objc public func nextPageButtonTapped(){
        if let offset_x = self.scrollView?.contentOffset.x {
            if Int(offset_x) % Int( self.scrollViewFrame.width) == 0 {
                let page = Int(offset_x / self.scrollViewFrame.width)
                if page + 1 < self.images.count {
                    UIView.animate(withDuration: 0.3) {
                        self.scrollView?.contentOffset.x = self.scrollView!.frame.width * CGFloat(page+1)
                        self.pageControl?.currentPage = page+1
                    }
                }else{
                    self.close()
                }
            }
        }
    }

    private func makeScrollView() -> UIScrollView {
        let scroll_view = UIScrollView(frame: self.scrollViewFrame)
        scroll_view.contentSize = CGSize(
            width: self.scrollViewFrame.width * CGFloat(self.images.count),
            height: self.scrollViewFrame.height)
        scroll_view.isPagingEnabled = true
        for i in 0..<self.images.count {
            let image = UIImageView(image: UIImage(named: self.images[i]))
            image.frame = CGRect( x: self.scrollViewFrame.width * CGFloat(i),
                                  y: 0,
                                  width: self.scrollViewFrame.width,
                                  height: self.scrollViewFrame.height)
            image.backgroundColor = color[i]
            scroll_view.addSubview(image)
        }
        
        return scroll_view
    }
    
    private func makePageControl() -> UIPageControl {
        let page = UIPageControl(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 100, height: 50)))
        page.center = CGPoint(x: self.scrollViewFrame.midX, y: self.scrollViewFrame.maxY - 50)
        page.numberOfPages = self.images.count
        page.currentPage = 0
        
        return page
    }
    
    private func makeNextButton() -> UIButton {
        let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 100, height: 50)))
        button.center = CGPoint(x: self.scrollViewFrame.midX, y: self.scrollViewFrame.maxY - 100)
        button.backgroundColor = UIColor.white
        button.cornerRadius = 10
        button.setTitle("Next", for: .normal)
        button.setTitleColor(UIColor.black, for: .normal)
        button.addTarget(self, action: #selector(self.nextPageButtonTapped), for: .touchUpInside)
        return button
    }
    
    private func close() {
        print(#function)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if let offset_x = self.scrollView?.contentOffset.x {
            if Int(offset_x) % Int( self.scrollViewFrame.width) == 0 {
                let page = Int(offset_x / self.scrollViewFrame.width)
                self.pageControl?.currentPage = page
            }
        }
    }
    
}