2015-05-27 26 views
8

Tôi có một UIPageViewController, hoạt động tốt khi chúng tôi vuốt sang trái hoặc sang phải để lật trang.iOS Swift: UIPageViewController - Biến trang theo chương trình

class ViewController: UIViewController, UIPageViewControllerDataSource { 
... 
} 

Bây giờ tôi cũng dự định cung cấp nút Trước/Tiếp theo trên trang để các trang có thể được bật bằng cách nhấp vào các nút này.

Làm cách nào để kích hoạt thao tác vuốt sang trái/phải HOẶC lật trang theo chương trình?

LƯU Ý Đây là câu hỏi cho ngôn ngữ SWIFT, không phải Mục tiêu-C.

+0

thể trùng lặp của [? Có thể Rẽ trang lập trình trong UIPageViewController] (http://stackoverflow.com/questions/7208871/is -it-can-to-turn-page-program -atic-in-uipageviewcontroller) –

Trả lời

5

Sử dụng chức năng này và đặt kiểu chuyển đổi cho hoạt ảnh bạn muốn.

enter image description here

enter image description here

+1

JordanC> Tôi cần bật trang bằng cách nhấp vào nút - vì vậy làm cách nào để tôi sử dụng chức năng trên? – Jasper

+0

@Jasper Tạo một UIButton và khi nó được nhấp, bạn tìm ra trang nào tiếp theo trong UIPageViewController và chuyển trang đó vào phương thức setViewControllers. – JordanC

2

Đây là một thực hiện nhanh chóng của việc này:

private func slideToPage(index: Int, completion: (() -> Void)?) { 
    let count = //Function to get number of viewControllers 
    if index < count { 
     if index > currentPageIndex { 
      if let vc = viewControllerAtIndex(index) { 
       self.pageViewController.setViewControllers([vc], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: { (complete) -> Void in 
        self.currentPageIndex = index 
        completion?() 
       }) 
      } 
     } else if index < currentPageIndex { 
      if let vc = viewControllerAtIndex(index) { 
       self.pageViewController.setViewControllers([vc], direction: UIPageViewControllerNavigationDirection.Reverse, animated: true, completion: { (complete) -> Void in 
        self.currentPageIndex = index 
        completion?() 
       }) 
      } 
     } 
    } 
} 

viewControllerAtIndex(index: Int) -> UIViewController? là chức năng riêng của tôi để có được bộ điều khiển cái nhìn đúng đắn: vuốt để.

30

Thường không cần phải loay hoay xung quanh với chỉ mục trang và những gì không. Rất có thể bạn đang thực hiện một dataSource: UIPageViewControllerDataSource, có tất cả những gì bạn cần để có được một ViewController trước đó hoặc kế tiếp để hiển thị.

Swift 2.2 Ví dụ:

extension UIPageViewController { 

    func goToNextPage(){ 

     guard let currentViewController = self.viewControllers?.first else { return } 

     guard let nextViewController = dataSource?.pageViewController(self, viewControllerAfterViewController: currentViewController) else { return } 

     setViewControllers([nextViewController], direction: .Forward, animated: false, completion: nil) 

    } 


    func goToPreviousPage(){ 

     guard let currentViewController = self.viewControllers?.first else { return } 

     guard let previousViewController = dataSource?.pageViewController(self, viewControllerBeforeViewController: currentViewController) else { return } 

     setViewControllers([previousViewController], direction: .Reverse, animated: false, completion: nil) 

    } 

} 

Bằng cách này bạn sẽ được bảo đảm rằng các trang bạn đang chuyển sang là chính xác những gì của PageViewController xây dựng vào cử chỉ sẽ kích hoạt.

+2

Tôi đang gặp khó khăn trong việc tìm cách cập nhật 'presentationIndexForPageViewController' bằng cách sử dụng ý tưởng này. Trên 'pageItemViewController' của tôi, tôi đã có một biến được khai báo có tên là' itemIndex'. Làm thế nào tôi có thể đặt nó trong phần mở rộng này? – Adrian

+0

Tôi đã dùng func goToNextPage() với bộ hẹn giờ - Vậy làm cách nào tôi có thể sử dụng phương pháp này để cuộn đến trang tiếp theo bằng bộ hẹn giờ? đây là liên kết của câu hỏi của tôi https://stackoverflow.com/questions/45025088/change-pages-in-uipageviewcontroller-automatically-with-timer-in-swift-3?noredirect=1#comment77027842_45025088 –

+0

Lifesaver !!! Cám ơn rất nhiều. Tôi sẽ đăng phiên bản Swift 3 chỉ để tham khảo trong tương lai. – Gix

2

Các Swift 3 phiên bản của câu trả lời thatjuan 's (với một tinh chỉnh nhỏ để tham số animated như tôi cần nó để được hoạt hình theo mặc định, nhưng vẫn tham gia một tham số trong hàm) trong trường hợp bất cứ ai cần nó:

extension UIPageViewController { 

    func goToNextPage(animated: Bool = true) { 
     guard let currentViewController = self.viewControllers?.first else { return } 
     guard let nextViewController = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) else { return } 
     setViewControllers([nextViewController], direction: .forward, animated: animated, completion: nil) 
    } 

    func goToPreviousPage(animated: Bool = true) { 
     guard let currentViewController = self.viewControllers?.first else { return } 
     guard let previousViewController = dataSource?.pageViewController(self, viewControllerBefore: currentViewController) else { return } 
     setViewControllers([previousViewController], direction: .reverse, animated: animated, completion: nil) 
    } 

} 
0

Chỉ cần bỏ qua điều này ở đây trong trường hợp bất kỳ ai muốn sử dụng giao thức được xác định để điều hướng và thay đổi bộ điều khiển chế độ xem theo chương trình.

@objc protocol AppWalkThroughDelegate { 
    @objc optional func goNextPage(forwardTo position: Int) 
    @objc optional func goPreviousPage(fowardTo position: Int) 
} 

Sử dụng giao thức ở trên và xác nhận quyền root UIPageViewController để quản lý điều hướng giữa bộ điều khiển chế độ xem.

Ví dụ dưới đây:

class AppWalkThroughViewController: UIPageViewController, UIPageViewControllerDataSource, AppWalkThroughDelegate { 

    // Add list of view controllers you want to load 

    var viewControllerList : [UIViewControllers] = { 
     let firstViewController = FirstViewController() 
     let secondViewController = SecondViewController() 
     // Assign root view controller as first responder 
     secondViewController.delegate = self 

     let thirdViewController = ThirdViewController() 

    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.dataSource = self 
    } 

    // Navigate to next page 
    func goNextPage(fowardTo position: Int) { 
     let viewController = self.viewControllerList[position] 
     setViewControllers([viewController], direction: 
     UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil) 
    } 

    } 

Khi đạt được tất cả những gì, nhìn con điều khiển mà cần phải thực hiện UIPageViewController chuyển sang trang tiếp theo hoặc trước đó có thể sử dụng phương pháp AppWalkThroughDelegate bằng cách thông qua một số cụ thể vào bất động sản đại biểu.

Ví dụ dưới đây: phương pháp đại biểu gọi một lần nút nhấn

class SecondViewController: UIViewController { 

     var delegate: AppWalkThroughDelegate! 

     override func viewDidLoad() { 
      super.viewDidLoad() 
      self.dataSource = self 
     } 

     @IBAction func goNextPage(_ sender: Any) { 
      // Can be any number but not outside viewControllerList bounds 
      delegate.goNextPage!(fowardTo: 2) 
     } 
    } 
Các vấn đề liên quan