MTImagePickerPreviewController.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // ImageSelectorPreviewController.swift
  3. // CMBMobile
  4. //
  5. // Created by Luo on 5/11/16.
  6. // Copyright © 2016 Yst-WHB. All rights reserved.
  7. //
  8. import UIKit
  9. import AVFoundation
  10. class MTImagePickerPreviewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
  11. var dataSource: [MTImagePickerModel]!
  12. var selectedSource: Set<MTImagePickerModel>!
  13. var initialIndexPath: IndexPath?
  14. var maxCount: Int!
  15. var dismiss: ((Set<MTImagePickerModel>) -> Void)?
  16. @IBOutlet weak var topView: UIView!
  17. @IBOutlet weak var collectionView: MTImagePickerCollectionView!
  18. @IBOutlet weak var bottomView: UIView!
  19. @IBOutlet weak var btnCheck: UIButton!
  20. @IBOutlet weak var lbSelected: UILabel!
  21. private var initialScrollDone = false
  22. class var instance: MTImagePickerPreviewController {
  23. get {
  24. let storyboard = UIStoryboard(name: "MTImagePicker", bundle: Bundle.main)
  25. let vc = storyboard.instantiateViewController(withIdentifier: "MTImagePickerPreviewController") as! MTImagePickerPreviewController
  26. return vc
  27. }
  28. }
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. self.lbSelected.text = String(self.selectedSource.count)
  32. }
  33. override func viewWillAppear(_ animated: Bool) {
  34. self.navigationController?.isNavigationBarHidden = true
  35. }
  36. override var prefersStatusBarHidden: Bool {
  37. get {
  38. return true
  39. }
  40. }
  41. override func viewDidAppear(_ animated: Bool) {
  42. self.scrollViewDidEndDecelerating(self.collectionView)
  43. }
  44. override func viewWillDisappear(_ animated: Bool) {
  45. self.navigationController?.isNavigationBarHidden = false
  46. }
  47. override func viewDidLayoutSubviews() {
  48. super.viewDidLayoutSubviews()
  49. if !self.initialScrollDone {
  50. self.initialScrollDone = true
  51. if let initialIndexPath = self.initialIndexPath {
  52. self.collectionView.scrollToItem(at: initialIndexPath, at: .right, animated: false)
  53. }
  54. }
  55. }
  56. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  57. return dataSource.count
  58. }
  59. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  60. let model = self.dataSource[indexPath.row]
  61. self.btnCheck.isSelected = self.selectedSource.contains(model)
  62. if model.mediaType == .Photo {
  63. let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath as IndexPath) as! ImagePickerPreviewCell
  64. cell.layer.shouldRasterize = true
  65. cell.layer.rasterizationScale = UIScreen.main.scale
  66. cell.initWithModel(model, controller: self)
  67. return cell
  68. } else {
  69. let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "VideoCell", for: indexPath as IndexPath) as! VideoPickerPreviewCell
  70. cell.layer.shouldRasterize = true
  71. cell.layer.rasterizationScale = UIScreen.main.scale
  72. cell.initWithModel(model: model, controller: self)
  73. return cell
  74. }
  75. }
  76. // 旋转处理
  77. override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
  78. if self.interfaceOrientation.isPortrait != toInterfaceOrientation.isPortrait {
  79. if let videoCell = self.collectionView.visibleCells.first as? VideoPickerPreviewCell {
  80. // CALayer 无法autolayout 需要重设frame
  81. videoCell.resetLayer(frame: UIScreen.main.compatibleBounds)
  82. }
  83. self.collectionView.prevItemSize = (self.collectionView.collectionViewLayout as! MTImagePickerPreviewFlowLayout).itemSize
  84. self.collectionView.prevOffset = self.collectionView.contentOffset.x
  85. self.collectionView.collectionViewLayout.invalidateLayout()
  86. }
  87. }
  88. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  89. return CGSize(width: self.collectionView.bounds.width, height: self.collectionView.bounds.height)
  90. }
  91. // MARK: UIScrollViewDelegate
  92. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  93. if let videoCell = self.collectionView.visibleCells.first as? VideoPickerPreviewCell {
  94. videoCell.didScroll()
  95. }
  96. }
  97. //防止visibleCells出现两个而不是一个,导致.first得到的是未显示的cell
  98. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  99. self.perform(#selector(MTImagePickerPreviewController.didEndDecelerating), with: nil, afterDelay: 0)
  100. }
  101. @objc func didEndDecelerating() {
  102. let cell = self.collectionView.visibleCells.first
  103. if let videoCell = cell as? VideoPickerPreviewCell {
  104. videoCell.didEndScroll()
  105. } else if let imageCell = cell as? ImagePickerPreviewCell {
  106. imageCell.didEndScroll()
  107. }
  108. }
  109. @IBAction func btnBackTouch(_ sender: AnyObject) {
  110. self.dismiss?(self.selectedSource)
  111. _ = self.navigationController?.popViewController(animated: true)
  112. }
  113. @IBAction func btnCheckTouch(_ sender: UIButton) {
  114. if self.selectedSource.count < self.maxCount || sender.isSelected == true {
  115. sender.isSelected = !sender.isSelected
  116. if let indexPath = self.collectionView.indexPathsForVisibleItems.first {
  117. let model = self.dataSource[indexPath.row]
  118. if sender.isSelected {
  119. self.selectedSource.insert(model)
  120. sender.heartbeatsAnimation(duration: 0.15)
  121. } else {
  122. self.selectedSource.remove(model)
  123. }
  124. self.lbSelected.text = String(self.selectedSource.count)
  125. self.lbSelected.heartbeatsAnimation(duration: 0.15)
  126. }
  127. } else {
  128. let alertView = FlashAlertView(message: "Maxium selected".localized, delegate: nil)
  129. alertView.show()
  130. }
  131. }
  132. }