优惠券swift版马甲包

MFPageContentView.swift 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // MFPageContentView.swift
  3. // MFPagingViewExample
  4. // GitHub: https://github.com/wwx1991/MFPagingView
  5. // Created by iOS on 2018/6/12.
  6. // Copyright © 2018年 GM. All rights reserved.
  7. //
  8. protocol MFPageContentViewDelegate: NSObjectProtocol {
  9. func pageContentViewScroll(progress: CGFloat, originalIndex: Int, targetIndex: Int)
  10. }
  11. import UIKit
  12. class MFPageContentView: UIView {
  13. weak var pageContentViewDelegate: MFPageContentViewDelegate?
  14. // 外界父控制器
  15. weak private var parentViewController: UIViewController?
  16. // 存储子控制器
  17. private var childViewControllers = [UIViewController]()
  18. //记录加载的上一个控制器
  19. private var lastVC: UIViewController?
  20. //记录刚开始时的偏移量
  21. private var startOffsetX: CGFloat = 0
  22. //scrollView
  23. private lazy var scrollView: UIScrollView = {
  24. let scrollView = UIScrollView(frame: self.bounds)
  25. scrollView.delegate = self
  26. scrollView.bounces = false
  27. scrollView.isPagingEnabled = true
  28. scrollView.showsVerticalScrollIndicator = false
  29. scrollView.showsHorizontalScrollIndicator = false
  30. scrollView.contentSize = CGSize(width: CGFloat(self.childViewControllers.count) * self.width, height: 0)
  31. return scrollView
  32. }()
  33. convenience init(frame: CGRect, parentVC: UIViewController, childVCs: Array<UIViewController>) {
  34. self.init(frame: frame)
  35. self.parentViewController = parentVC
  36. self.childViewControllers = childVCs
  37. setupSubViews()
  38. }
  39. private func setupSubViews() {
  40. //处理偏移量
  41. let tempView = UIView(frame: CGRect.zero)
  42. self.addSubview(tempView)
  43. self.addSubview(scrollView)
  44. }
  45. }
  46. extension MFPageContentView: UIScrollViewDelegate {
  47. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  48. self.startOffsetX = scrollView.contentOffset.x
  49. }
  50. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  51. if self.lastVC != nil {
  52. self.lastVC?.beginAppearanceTransition(false, animated: false)
  53. self.lastVC?.endAppearanceTransition()
  54. }
  55. let offsetX: CGFloat = scrollView.contentOffset.x
  56. let index: Int = Int(offsetX / scrollView.frame.size.width)
  57. let childVC: UIViewController = self.childViewControllers[index]
  58. self.parentViewController?.addChildViewController(childVC)
  59. childVC.beginAppearanceTransition(true, animated: false)
  60. self.scrollView.addSubview(childVC.view)
  61. childVC.endAppearanceTransition()
  62. //记录上个展示的子控制器
  63. self.lastVC = childVC
  64. childVC.view.frame = CGRect(x: offsetX, y: 0, width: self.width, height: self.height)
  65. }
  66. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  67. var progress: CGFloat = 0
  68. var originalIndex: Int = 0
  69. var targetIndex: Int = 0
  70. //判断是左滑还是右滑
  71. let currentOffsetX: CGFloat = scrollView.contentOffset.x
  72. let scrollViewW: CGFloat = scrollView.width
  73. if currentOffsetX > self.startOffsetX { // 左滑
  74. progress = currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW)
  75. originalIndex = Int(currentOffsetX / scrollViewW)
  76. targetIndex = originalIndex + 1
  77. if targetIndex >= self.childViewControllers.count {
  78. progress = 1
  79. targetIndex = self.childViewControllers.count - 1
  80. }
  81. //如果完全划过去
  82. if currentOffsetX - self.startOffsetX == scrollViewW {
  83. progress = 1
  84. targetIndex = originalIndex
  85. }
  86. } else { // 右滑
  87. progress = 1 - (currentOffsetX / scrollViewW - floor(currentOffsetX / scrollViewW))
  88. targetIndex = Int(currentOffsetX / scrollViewW)
  89. originalIndex = targetIndex + 1
  90. if originalIndex >= self.childViewControllers.count {
  91. originalIndex = self.childViewControllers.count - 1
  92. }
  93. }
  94. pageContentViewDelegate?.pageContentViewScroll(progress: progress, originalIndex: originalIndex, targetIndex: targetIndex)
  95. }
  96. }
  97. extension MFPageContentView {
  98. //给外界用以获取MFPageTitleView选中按钮的下标
  99. func setPageContentViewCurrentIndex(currentIndex: NSInteger) {
  100. if self.lastVC != nil {
  101. self.lastVC?.beginAppearanceTransition(false, animated: false)
  102. self.lastVC?.endAppearanceTransition()
  103. }
  104. let offsetX: CGFloat = CGFloat(currentIndex) * self.width
  105. //添加子控制器以及子控制器的 view
  106. let childVC: UIViewController = self.childViewControllers[currentIndex]
  107. self.parentViewController?.addChildViewController(childVC)
  108. childVC.beginAppearanceTransition(true, animated: false)
  109. self.scrollView.addSubview(childVC.view)
  110. childVC.endAppearanceTransition()
  111. childVC.view.frame = CGRect(x: offsetX, y: 0, width: self.width, height: self.height)
  112. //记录上个展示的子控制器
  113. self.lastVC = childVC
  114. //处理内容偏移
  115. self.scrollView.contentOffset = CGPoint(x: offsetX, y: 0)
  116. }
  117. }