2017年3月14日 星期二

pageviewcontroller



先在story.board 新增一個 pageviewcontrller 和  一個view-controller

新增一個 cocoa Touch file , 叫做   ContentViewController , subclass ->  UIViewController


然後拉一個 image view 進去..  新增一個 變數

class ContentViewController: UIViewController {

    var nowPageNumber = 0
    
    @IBOutlet weak var myImageView: UIImageView!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        myImageView.image = UIImage(named: "\(nowPageNumber)")
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */


}




在這個新增的viewcontroller 設定他的 storyboardID ->  mainContentViewController






class ViewController: UIViewController {

    var pageViewController:UIPageViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        pageViewController =  storyboard.instantiateViewController(withIdentifier: "pageViewController") as? UIPageViewController
        
        pageViewController?.view.frame = self.view.frame
        

       // addChildViewController() 裡面的參數不能是 optional的物件, 所以要加!
         addChildViewController(pageViewController!)

        // 把 pageviewcontroller 加到目前的viewcontroller.  當作一個子類別的viewcontroller

        //addSubview() 裡面的參數不能是 optional的物件, 所以要加!
        view.addSubview(pageViewController!.view)

        // 把  pageViewController.view 加到目前的畫面上

        pageViewController?.didMove(toParentViewController: self)

        

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func viewControllerAtIndex(index:Int) -> ContentViewController?{
       let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let contentviewController =  storyboard.instantiateViewController(withIdentifier: "mainContentViewController") asContentViewController
           
        contentviewController?.nowPageNumber = index
        return contentviewController
    }


}


-----------------------------

加入翻頁的程式碼

-----------------------------


 func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let vc = viewController as? ContentViewController
        guard var index = vc?.nowPageNumber else{return nil}
        if index == 0 || index == NSNotFound {
            return nil
        }else{
            index -= 1
            return  viewControllerAtIndex(index : index)
        }
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let vc = viewController as? ContentViewController
        guard var index = vc?.nowPageNumber else{return nil}
        if index == NSNotFound {
            return nil
        }else{
            index += 1
            if index >= 3{
                return nil
            }else{
                return  viewControllerAtIndex(index : index)
            }
        }
    }







沒有留言:

張貼留言