###前提・実現したいこと
PageViewControllerでアプリ初回起動時のウォークスルーを作っています。
pageControllにてスワイプで画面移動することはできたのですが、
「次へ」、「戻る」ボタンを設置し、押した時に、スワイプと同じようにスクロールして画面を移動させる方法がわかりません。
###該当のソースコード
Swift
1// PageViewController 2import UIKit 3 4class IntroPageViewController: UIPageViewController { 5 6 var pageViewControllerArray: [UIViewController] = [] 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 self.dataSource = self 12 13 // 各ページをインスタンス化 14 let selectHireDateVC = storyboard?.instantiateViewController(withIdentifier: "selectHireDateVC") as! SelectHireDateVC 15 let selectWorkingDaysVC = storyboard?.instantiateViewController(withIdentifier: "selectWorkingDaysVC") as! SelectWorkingDaysVC 16 let selectRemainingDaysVC = storyboard?.instantiateViewController(withIdentifier: "selectRemainingDaysVC") as! SelectRemainigDaysVC 17 18 /// 全ページが入った配列 19 pageViewControllerArray = [selectHireDateVC, selectWorkingDaysVC, selectRemainingDaysVC] 20 21 // UIPageControllerに表示対象を追加 22 setViewControllers([pageViewControllerArray[0]], direction: .forward, animated: false, completion: nil) 23 24 // PageControllの色を変更する 25 let pageControll = UIPageControl.appearance() 26 pageControll.backgroundColor = UIColor.white 27 pageControll.pageIndicatorTintColor = UIColor.lightGray 28 pageControll.currentPageIndicatorTintColor = UIColor.darkGray 29 } 30 31 /// ページ数を返すメソッド 32 func presentationCount(for pageViewController: UIPageViewController) -> Int { 33 return pageViewControllerArray.count 34 } 35 36 func presentationIndex(for pageViewController: UIPageViewController) -> Int { 37 return 0 38 } 39} 40 41extension IntroPageViewController: UIPageViewControllerDataSource { 42 43 /// 右にスワイプ(戻る)した場合のメソッド 44 func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { 45 // 今表示しているページ数を取得する 46 let index = pageViewControllerArray.index(of: viewController) 47 // 最初のページでなければページを一つ戻す 48 if index != 0 { 49 return pageViewControllerArray[index! - 1] 50 } else { 51 return nil 52 } 53 } 54 55 /// 左にスワイプ(進む)した場合のメソッド 56 func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { 57 // 今表示しているページ数を取得する 58 let index = pageViewControllerArray.index(of: viewController) 59 // 最終ページでなければページを一つ進める 60 if index != pageViewControllerArray.count - 1 { 61 return pageViewControllerArray[index! + 1] 62 } else { 63 return nil 64 } 65 } 66}
Swift
1// 1ページ目のViewController 2import UIKit 3 4class SelectHireDateVC: UIViewController { 5 6 @IBOutlet weak var hireDatePicker: UIDatePicker! 7 8 @IBAction func moveToNextPage(_ sender: UIButton) { 9// 10 // 各ページをインスタンス化 11 let selectHireDateVC = storyboard?.instantiateViewController(withIdentifier: "selectHireDateVC") as! SelectHireDateVC 12 let selectWorkingDaysVC = storyboard?.instantiateViewController(withIdentifier: "selectWorkingDaysVC") as! SelectWorkingDaysVC 13 let selectRemainingDaysVC = storyboard?.instantiateViewController(withIdentifier: "selectRemainingDaysVC") as! SelectRemainigDaysVC 14 15 /// 全ページが入った配列 16 let pageViewControllerArray = [selectHireDateVC, selectWorkingDaysVC, selectRemainingDaysVC] 17 18 let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil) 19 pageViewController.view.frame = view.bounds.insetBy(dx: CGFloat(200), dy: CGFloat(200)) 20 view.addSubview(pageViewController.view) 21 pageViewController.setViewControllers([pageViewControllerArray[1]], direction: .forward, animated: true, completion: nil) 22 } 23 24 override func viewDidLoad() { 25 super.viewDidLoad() 26 } 27} 28
###試したこと
https://stackoverflow.com/questions/23835805/ios-uipageviewcontroller-use-button-to-jump-to-next-page
など、それらしい方法が書いてあるページは見つけたのですが、当方の理解力では実装することができませんでした。
よろしくお願いいたします。
###補足情報(言語/FW/ツール等のバージョンなど)
Xcode 8.3.3 Swift 3.1

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。