Created
December 3, 2018 05:48
-
-
Save victorchee/9aa6c566f6e3e282212e43cdfac1367f to your computer and use it in GitHub Desktop.
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: UIViewController { | |
@IBOutlet weak var stackView: UIStackView! | |
@IBOutlet weak var stackViewIndicatorLeadingConstraint: NSLayoutConstraint! | |
fileprivate var previousStackViewIndicatorLeadingConstraintConstant: CGFloat = 0 | |
fileprivate var selectedButton: UIButton? | |
var pageViewController: UIPageViewController? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// ...... | |
if let controller = viewControllerForPage(at: 1) { | |
pageViewController?.setViewControllers([controller], direction: .forward, animated: false) { (_) in | |
self.selectedButton = self.stackView.arrangedSubviews[1] as? UIButton | |
self.stackViewIndicatorLeadingConstraint.constant = 84 // 初始值 | |
self.previousStackViewIndicatorLeadingConstraintConstant = self.stackViewIndicatorLeadingConstraint.constant | |
} | |
} | |
if let scrollView = pageViewController?.view.subviews.filter({ $0 is UIScrollView }).first as? UIScrollView { | |
scrollView.delegate = self | |
} | |
} | |
} | |
extension ViewController: UIScrollViewDelegate { | |
func scrollViewDidScroll(_ scrollView: UIScrollView) { | |
let offsetX = scrollView.contentOffset.x | |
var percentage: CGFloat = 0 | |
if offsetX == scrollView.frame.width { | |
// Page ended | |
previousStackViewIndicatorLeadingConstraintConstant = self.stackViewIndicatorLeadingConstraint.constant // 重置 | |
} else { | |
// Paging | |
percentage = (offsetX - scrollView.frame.width) / scrollView.frame.width | |
if percentage > 0 { | |
// Next paging | |
} else { | |
// Previous paging | |
} | |
self.stackViewIndicatorLeadingConstraint.constant = previousStackViewIndicatorLeadingConstraintConstant + (84 - 29) * percentage | |
print(percentage) | |
self.stackView.layoutIfNeeded() | |
} | |
} | |
} | |
extension ViewController: UIPageViewControllerDelegate { | |
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) { } | |
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { | |
guard let currentController = pageViewController.viewControllers?.first else { return } | |
let index = indexForPage(of: currentController) | |
selectedButton?.isSelected = false | |
let pendingButton = stackView.arrangedSubviews[index] as! UIButton | |
pendingButton.isSelected = true | |
selectedButton = pendingButton | |
self.stackViewIndicatorLeadingConstraint.constant = selectedButton?.tag == 0 ? 29 : 84 // 动画完成,确定位置 | |
self.stackView.layoutIfNeeded() | |
UIView.animate(withDuration: 0.25) { | |
self.navigationView.layoutIfNeeded() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment