Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIPageViewController #20

Closed
MoSonLee opened this issue Aug 16, 2022 · 0 comments
Closed

UIPageViewController #20

MoSonLee opened this issue Aug 16, 2022 · 0 comments

Comments

@MoSonLee
Copy link
Owner

  • 여러 개의 뷰를 페이징 처럼 넘기면서 볼 때 사용.
  • 사용법
    • 뷰 컨트롤러 배열 선언
var pageViewControllerList: [UIViewController] = [] 
  • 배열에 뷰 컨틀로러를 추가해줌
         func createPageViewController() {
          let sb = UIStoryboard(name: "WalkThrough", bundle: nil)
          let vc1 = sb.instantiateViewController(withIdentifier: FirstViewController.reuseIdentifier) as! FirstViewController
          let vc2 = sb.instantiateViewController(withIdentifier: SecondViewController .reuseIdentifier) as! SecondViewController
          let vc3 = sb.instantiateViewController(withIdentifier: ThirdViewController.reuseIdentifier) as! ThirdViewController
          pageViewControllerList = [vc1, vc2, vc3]
    }
  • extension 추가
extension PageViewController: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        // 현재 페이지뷰컨트롤러에 보이는 뷰컨(ViewController)의 인덱스 가져오기
        guard let viewControllerIndex = pageViewControllerList.firstIndex(of: viewController) else { return nil }
        
        let previousIndex = viewControllerIndex - 1
        
        return previousIndex < 0 ? nil : pageViewControllerList[previousIndex]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = pageViewControllerList.firstIndex(of: viewController) else { return nil }
        
        let nextIndex = viewControllerIndex + 1
        
        return nextIndex >= pageViewControllerList.count ? nil : pageViewControllerList[nextIndex]
    }
    
    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return pageViewControllerList.count
    }
    
    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        guard let first = viewControllers?.first, let index = pageViewControllerList.firstIndex(of: first) else { return 0 }
    
        return index
    }
}
  • 위에 presentationCount, presentaitonIndex는 무슨 코드일까?

    • image
    • 이것처럼 밑에 몇번째 페이지인지 확인할 수 있는 것을 띄워주는 함수!
  • display

func configurePageViewController() {
       delegate = self
       dataSource = self
       // display
       guard let first = pageViewControllerList.first else { return }
       setViewControllers([first], direction: .forward, animated: true)
   }
  • 자 그럼 순서를 다시 복기해보자
    • 위에 코드에선 생략했지만 pageViewController 한개, 넘겨서 보여줄 view Controller들이 필요하다
    • 뷰컨들을 생성해준 후
    • 뷰컨트롤러 배열을 만들어서 거기에 뷰컨트롤러들을 추가해줌
    • extension에 페이지뷰에 보여줄 뷰컨들의 인덱스를 가져오는 등 pageView를 보여주기 위한 작업을 하고
    • delegate, datasource를 self로 초기화해준 후
    • display하는 코드를 작성하면 끝
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant