- Chỉ cần vào property -- > un check Under Top Bar
Search This Blog
Hiển thị các bài đăng có nhãn PageViewController. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn PageViewController. Hiển thị tất cả bài đăng
Chủ Nhật, 10 tháng 9, 2017
Thứ Sáu, 8 tháng 9, 2017
Tạo PageViewController trong IOS
- TutorialPageViewController.swift :
//
// TutorialPageViewController.swift
// UIPageViewController Post
//
// Created by Jeffrey Burt on 12/11/15.
// Copyright © 2015 Atomic Object. All rights reserved.
//
import UIKit
class TutorialPageViewController: UIPageViewController {
weak var tutorialDelegate: TutorialPageViewControllerDelegate?
fileprivate(set) lazy var orderedViewControllers: [UIViewController] = {
// Thêm page
return [self.newColoredViewController("Green","1"),
self.newColoredViewController("Green","2"),
self.newColoredViewController("Green","3"),
self.newColoredViewController("Green","4")
]
}()
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
delegate = self
if let initialViewController = orderedViewControllers.first {
scrollToViewController(initialViewController)
}
// cap nhật số page
tutorialDelegate?.tutorialPageViewController(self,
didUpdatePageCount: orderedViewControllers.count)
}
/**
Scrolls to the next view controller.
*/
func scrollToNextViewController() {
if let visibleViewController = viewControllers?.first,
let nextViewController = pageViewController(self,
viewControllerAfter: visibleViewController) {
scrollToViewController(nextViewController)
}
}
/**
Scrolls to the view controller at the given index. Automatically calculates
the direction.
- parameter newIndex: the new index to scroll to
*/
func scrollToViewController(index newIndex: Int) {
if let firstViewController = viewControllers?.first,
let currentIndex = orderedViewControllers.index(of: firstViewController) {
let direction: UIPageViewControllerNavigationDirection = newIndex >= currentIndex ? .forward : .reverse
let nextViewController = orderedViewControllers[newIndex]
scrollToViewController(nextViewController, direction: direction)
}
}
// gọi tới idIStoryboard trả về ViewController
fileprivate func newColoredViewController(_ color: String , _ idz:String) -> UIViewController {
let sb = UIStoryboard(name: "Main", bundle: nil)
let manhinh = sb.instantiateViewController(withIdentifier: "\(color)ViewController") as! ChiTietViewController
manhinh.id = idz
return manhinh
}
/**
sự kiện Vuốt
*/
fileprivate func scrollToViewController(_ viewController: UIViewController,
direction: UIPageViewControllerNavigationDirection = .forward) {
setViewControllers([viewController],
direction: direction,
animated: true,
completion: { (finished) -> Void in
// Setting the view controller programmatically does not fire
// any delegate methods, so we have to manually notify the
// 'tutorialDelegate' of the new index.
self.notifyTutorialDelegateOfNewIndex()
})
}
/**
Notifies '_tutorialDelegate' that the current page index was updated.
*/
fileprivate func notifyTutorialDelegateOfNewIndex() {
if let firstViewController = viewControllers?.first,
let index = orderedViewControllers.index(of: firstViewController) {
tutorialDelegate?.tutorialPageViewController(self,
didUpdatePageIndex: index)
}
}
}
// MARK: UIPageViewControllerDataSource
extension TutorialPageViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
// User is on the first view controller and swiped left to loop to
// the last view controller.
guard previousIndex >= 0 else {
return orderedViewControllers.last
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
// User is on the last view controller and swiped right to loop to
// the first view controller.
guard orderedViewControllersCount != nextIndex else {
return orderedViewControllers.first
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
}
extension TutorialPageViewController: UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController,
didFinishAnimating finished: Bool,
previousViewControllers: [UIViewController],
transitionCompleted completed: Bool) {
notifyTutorialDelegateOfNewIndex()
}
}
protocol TutorialPageViewControllerDelegate: class {
/**
tong so page
*/
func tutorialPageViewController(_ tutorialPageViewController: TutorialPageViewController,
didUpdatePageCount count: Int)
/**
gia tri page hien tai
*/
func tutorialPageViewController(_ tutorialPageViewController: TutorialPageViewController,
didUpdatePageIndex index: Int)
}
- TutorialViewController.swift :
//
// TutorialViewController.swift
// UIPageViewController Post
//
// Created by Jeffrey Burt on 2/3/16.
// Copyright © 2016 Seven Even. All rights reserved.
//
import UIKit
class TutorialViewController: UIViewController {
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var containerView: UIView!
var tutorialPageViewController: TutorialPageViewController? {
didSet {
tutorialPageViewController?.tutorialDelegate = self
}
}
override func viewDidLoad() {
super.viewDidLoad()
// thêm sự kiện thay đổi nút
pageControl.addTarget(self, action: #selector(self.didChangePageControlValue), for: .valueChanged)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let tutorialPageViewController = segue.destination as? TutorialPageViewController {
self.tutorialPageViewController = tutorialPageViewController
}
}
// sự kiện click button next page
@IBAction func didTapNextButton(_ sender: UIButton) {
tutorialPageViewController?.scrollToNextViewController()
}
/**
Fired when the user taps on the pageControl to change its current page.
*/
// click icon tron
func didChangePageControlValue() {
tutorialPageViewController?.scrollToViewController(index: pageControl.currentPage)
}
}
extension TutorialViewController: TutorialPageViewControllerDelegate {
// tổng số nút
func tutorialPageViewController(_ tutorialPageViewController: TutorialPageViewController,
didUpdatePageCount count: Int) {
pageControl.numberOfPages = count
}
// nút hiện tại
func tutorialPageViewController(_ tutorialPageViewController: TutorialPageViewController,
didUpdatePageIndex index: Int) {
pageControl.currentPage = index
}
}
- tải : https://github.com/jeffaburt/UIPageViewController-Post
- tải : https://github.com/jeffaburt/UIPageViewController-Post
Đăng ký:
Bài đăng (Atom)


