UIPageViewController / UIPageControl


PageViewController와 PageControl의 조합 형태를 만든다.



다음과 같이 PageViewController와 자식이 될 ViewController를 준비한다.



PageViewController.swift


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class ViewController_tutorialRootView: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource
{
    var index : Int = 0;
    var indexMin = 0;
    var indexMax = 2;
    
    // restoreIdentifier
    var identifiers: NSArray = ["ViewController_Tutorial_1""ViewController_Tutorial_2""ViewController_Tutorial_3"]
    
    // The custom UIPageControl
    let pageControl = UIPageControl()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.delegate = self
        self.dataSource = self
        
        index = 0
        self.setViewControllers([getViewControllerAtIndex(index: 0)] as! [UIViewController], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
        
        // PageControl 세팅
        self.pageControl.frame = CGRect()
        self.pageControl.currentPageIndicatorTintColor = UIColor.black
        self.pageControl.pageIndicatorTintColor = UIColor.lightGray
        self.pageControl.numberOfPages = self.identifiers.count
        self.pageControl.currentPage = 0
        self.view.addSubview(self.pageControl)
        
        self.pageControl.translatesAutoresizingMaskIntoConstraints = false
        self.pageControl.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -90).isActive = true
        self.pageControl.widthAnchor.constraint(equalTo: self.view.widthAnchor, constant: -20).isActive = true
        self.pageControl.heightAnchor.constraint(equalToConstant: 20).isActive = true
        self.pageControl.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    }
    
    // MARK:- UIPageViewControllerDataSource Methods
    // 이전 화면 그리기
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
    {        
        let identifier = viewController.restorationIdentifier
        var index = self.identifiers.index(of: identifier)
        
        NSLog("ViewController_tutorialRootView viewControllerBefore index : %d", index)
        
        if index == 0
        {
            return nil
        }
        
        //decrement the index to get the viewController before the current one
        self.index = index - 1
        
        return getViewControllerAtIndex (index: self.index)
    }
    
    // 다음 화면 그리기
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
    {
        
        
        let identifier = viewController.restorationIdentifier
        var index = self.identifiers.index(of: identifier)
        
        NSLog("ViewController_tutorialRootView viewControllerAfter index : %d", index)
        
        if (index == self.identifiers.count-1)
        {
            return nil
        }
        
        self.index = index + 1
        
        return getViewControllerAtIndex (index: self.index)
    }
    
    // 현재 페이지 구하기
    func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
        let currentPage : Int = self.identifiers.index(of: pendingViewControllers[0].restorationIdentifier)
        NSLog("ViewController_tutorialRootView willTransitionTo index = %d", currentPage)
        self.pageControl.currentPage = currentPage
    }
    
    // 인디케이터에 반영할 항목 갯수 반환
    func presentationCountForPageViewController(pageViewController: UIPageViewController!-> Int {
        return self.identifiers.count
    }
 
    // 페이지 인디케이터에 반영할 아이템 인덱스
    func presentationIndexForPageViewController(pageViewController: UIPageViewController!-> Int {
        return 0
    }
    
    
    
    // MARK:- Other Methods
    func getViewControllerAtIndex(index: NSInteger) -> UIViewController?
    {
        NSLog("ViewController_tutorialRootView getViewControllerAtIndex index : %d", index)
        
        if index == 0
        {
            let storyboard = UIStoryboard(name: "Tutorial", bundle: nil);
            let viewController_Tutorial_1 = storyboard.instantiateViewController(withIdentifier: "ViewController_Tutorial_1"as! ViewController_Tutorial_1
            return viewController_Tutorial_1
        }
        else if index == 1
        {
            
            let storyboard = UIStoryboard(name: "Tutorial", bundle: nil);
            let viewController_Tutorial_2 = storyboard.instantiateViewController(withIdentifier: "ViewController_Tutorial_2"as! ViewController_Tutorial_2
            return viewController_Tutorial_2
        }
        else if index == 2
        {
            let storyboard = UIStoryboard(name: "Tutorial", bundle: nil);
            let viewController_Tutorial_3 = storyboard.instantiateViewController(withIdentifier: "ViewController_Tutorial_3"as! ViewController_Tutorial_3
            return viewController_Tutorial_3
        }
        else
        {
            return nil
        }
    }
}
 
cs


restoreIdentifier를 사용하여 각 페이지를 구분하는 것이 포인트.


deleagate와 datasource 사용에 주의하자.




RestorationID 적용에 주의할 것.

이 글을 공유하기


댓글(0)

Designed by CMSFactory.NET