Created
March 25, 2015 05:42
-
-
Save kazukitanaka0611/9ca35034a75fd614cba6 to your computer and use it in GitHub Desktop.
UIPageViewController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIPageViewController, UIPageViewControllerDataSource { | |
private let imageNameList: [String] = ["ball.png", "neko.png"] | |
private var index: Int = 0 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
self.dataSource = self | |
if let initialViewController = self.viewControllerAtIndex(0) { | |
let viewControllers = [initialViewController] | |
self.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil) | |
} | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
override func shouldAutorotate() -> Bool { | |
return true | |
} | |
override func supportedInterfaceOrientations() -> Int { | |
return Int(UIInterfaceOrientationMask.All.rawValue) | |
} | |
func pageViewController(pageViewController: UIPageViewController, | |
viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? { | |
var i = (viewController as DetailViewController).index | |
return self.viewControllerAtIndex(i - 1) | |
} | |
func pageViewController(pageViewController: UIPageViewController, | |
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? { | |
var i = (viewController as DetailViewController).index | |
return self.viewControllerAtIndex(i + 1) | |
} | |
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int { | |
return self.imageNameList.count | |
} | |
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int { | |
return self.index | |
} | |
private func viewControllerAtIndex(index: Int) -> DetailViewController? { | |
if index < 0 || index > self.imageNameList.count - 1 { return nil } | |
let vc: DetailViewController = | |
self.storyboard?.instantiateViewControllerWithIdentifier("detail") as DetailViewController | |
self.index = index | |
vc.index = index | |
vc.imageName = self.imageNameList[index] | |
return vc | |
} | |
} | |
class DetailViewController: UIViewController { | |
@IBOutlet weak var imageView: UIImageView! | |
var index: Int = 0 | |
var imageName: String! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
self.imageView.image = UIImage(named: self.imageName) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment