StickersViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // StickersViewController.swift
  3. // Photo Editor
  4. //
  5. // Created by Mohamed Hamed on 4/23/17.
  6. // Copyright © 2017 Mohamed Hamed. All rights reserved.
  7. // Credit https://github.com/AhmedElassuty/IOS-BottomSheet
  8. import UIKit
  9. class StickersViewController: UIViewController, UIGestureRecognizerDelegate {
  10. @IBOutlet weak var headerView: UIView!
  11. @IBOutlet weak var holdView: UIView!
  12. @IBOutlet weak var scrollView: UIScrollView!
  13. @IBOutlet weak var pageControl: UIPageControl!
  14. var collectioView: UICollectionView!
  15. var emojisCollectioView: UICollectionView!
  16. var emojisDelegate: EmojisCollectionViewDelegate!
  17. var stickers : [UIImage] = []
  18. var stickersViewControllerDelegate : StickersViewControllerDelegate?
  19. let screenSize = UIScreen.main.bounds.size
  20. let fullView: CGFloat = 100 // remainder of screen height
  21. var partialView: CGFloat {
  22. return UIScreen.main.bounds.height - 380
  23. }
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. configureCollectionViews()
  27. scrollView.contentSize = CGSize(width: 2.0 * screenSize.width,
  28. height: scrollView.frame.size.height)
  29. scrollView.isPagingEnabled = true
  30. scrollView.delegate = self
  31. pageControl.numberOfPages = 2
  32. holdView.layer.cornerRadius = 3
  33. let gesture = UIPanGestureRecognizer.init(target: self, action: #selector(StickersViewController.panGesture))
  34. gesture.delegate = self
  35. view.addGestureRecognizer(gesture)
  36. }
  37. func configureCollectionViews() {
  38. let frame = CGRect(x: 0,
  39. y: 0,
  40. width: UIScreen.main.bounds.width,
  41. height: view.frame.height - 40)
  42. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
  43. layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
  44. let width = (CGFloat) ((screenSize.width - 30) / 3.0)
  45. layout.itemSize = CGSize(width: width, height: 100)
  46. collectioView = UICollectionView(frame: frame, collectionViewLayout: layout)
  47. collectioView.backgroundColor = .clear
  48. scrollView.addSubview(collectioView)
  49. collectioView.delegate = self
  50. collectioView.dataSource = self
  51. collectioView.register(
  52. UINib(nibName: "StickerCollectionViewCell", bundle: Bundle(for: StickerCollectionViewCell.self)),
  53. forCellWithReuseIdentifier: "StickerCollectionViewCell")
  54. //-----------------------------------
  55. let emojisFrame = CGRect(x: scrollView.frame.size.width,
  56. y: 0,
  57. width: UIScreen.main.bounds.width,
  58. height: view.frame.height - 40)
  59. let emojislayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
  60. emojislayout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
  61. emojislayout.itemSize = CGSize(width: 70, height: 70)
  62. emojisCollectioView = UICollectionView(frame: emojisFrame, collectionViewLayout: emojislayout)
  63. emojisCollectioView.backgroundColor = .clear
  64. scrollView.addSubview(emojisCollectioView)
  65. emojisDelegate = EmojisCollectionViewDelegate()
  66. emojisDelegate.stickersViewControllerDelegate = stickersViewControllerDelegate
  67. emojisCollectioView.delegate = emojisDelegate
  68. emojisCollectioView.dataSource = emojisDelegate
  69. emojisCollectioView.register(
  70. UINib(nibName: "EmojiCollectionViewCell", bundle: Bundle(for: EmojiCollectionViewCell.self)),
  71. forCellWithReuseIdentifier: "EmojiCollectionViewCell")
  72. }
  73. override func viewWillAppear(_ animated: Bool) {
  74. super.viewWillAppear(animated)
  75. prepareBackgroundView()
  76. }
  77. override func viewDidAppear(_ animated: Bool) {
  78. super.viewDidAppear(animated)
  79. UIView.animate(withDuration: 0.6) { [weak self] in
  80. guard let `self` = self else { return }
  81. let frame = self.view.frame
  82. let yComponent = self.partialView
  83. self.view.frame = CGRect(x: 0,
  84. y: yComponent,
  85. width: frame.width,
  86. height: UIScreen.main.bounds.height - self.partialView)
  87. }
  88. }
  89. override func viewDidLayoutSubviews() {
  90. super.viewDidLayoutSubviews()
  91. collectioView.frame = CGRect(x: 0,
  92. y: 0,
  93. width: UIScreen.main.bounds.width,
  94. height: view.frame.height - 40)
  95. emojisCollectioView.frame = CGRect(x: scrollView.frame.size.width,
  96. y: 0,
  97. width: UIScreen.main.bounds.width,
  98. height: view.frame.height - 40)
  99. scrollView.contentSize = CGSize(width: 2.0 * screenSize.width,
  100. height: scrollView.frame.size.height)
  101. }
  102. override func didReceiveMemoryWarning() {
  103. super.didReceiveMemoryWarning()
  104. }
  105. //MARK: Pan Gesture
  106. @objc func panGesture(_ recognizer: UIPanGestureRecognizer) {
  107. let translation = recognizer.translation(in: self.view)
  108. let velocity = recognizer.velocity(in: self.view)
  109. let y = self.view.frame.minY
  110. if y + translation.y >= fullView {
  111. let newMinY = y + translation.y
  112. self.view.frame = CGRect(x: 0, y: newMinY, width: view.frame.width, height: UIScreen.main.bounds.height - newMinY )
  113. self.view.layoutIfNeeded()
  114. recognizer.setTranslation(CGPoint.zero, in: self.view)
  115. }
  116. if recognizer.state == .ended {
  117. var duration = velocity.y < 0 ? Double((y - fullView) / -velocity.y) : Double((partialView - y) / velocity.y )
  118. duration = duration > 1.3 ? 1 : duration
  119. //velocity is direction of gesture
  120. UIView.animate(withDuration: duration, delay: 0.0, options: [.allowUserInteraction], animations: {
  121. if velocity.y >= 0 {
  122. if y + translation.y >= self.partialView {
  123. self.removeBottomSheetView()
  124. } else {
  125. self.view.frame = CGRect(x: 0, y: self.partialView, width: self.view.frame.width, height: UIScreen.main.bounds.height - self.partialView)
  126. self.view.layoutIfNeeded()
  127. }
  128. } else {
  129. if y + translation.y >= self.partialView {
  130. self.view.frame = CGRect(x: 0, y: self.partialView, width: self.view.frame.width, height: UIScreen.main.bounds.height - self.partialView)
  131. self.view.layoutIfNeeded()
  132. } else {
  133. self.view.frame = CGRect(x: 0, y: self.fullView, width: self.view.frame.width, height: UIScreen.main.bounds.height - self.fullView)
  134. self.view.layoutIfNeeded()
  135. }
  136. }
  137. }, completion: nil)
  138. }
  139. }
  140. func removeBottomSheetView() {
  141. UIView.animate(withDuration: 0.3,
  142. delay: 0,
  143. options: UIView.AnimationOptions.curveEaseIn,
  144. animations: { () -> Void in
  145. var frame = self.view.frame
  146. frame.origin.y = UIScreen.main.bounds.maxY
  147. self.view.frame = frame
  148. }, completion: { (finished) -> Void in
  149. self.view.removeFromSuperview()
  150. self.removeFromParent()
  151. self.stickersViewControllerDelegate?.stickersViewDidDisappear()
  152. })
  153. }
  154. func prepareBackgroundView(){
  155. let blurEffect = UIBlurEffect.init(style: .light)
  156. let visualEffect = UIVisualEffectView.init(effect: blurEffect)
  157. let bluredView = UIVisualEffectView.init(effect: blurEffect)
  158. bluredView.contentView.addSubview(visualEffect)
  159. visualEffect.frame = UIScreen.main.bounds
  160. bluredView.frame = UIScreen.main.bounds
  161. view.insertSubview(bluredView, at: 0)
  162. }
  163. }
  164. extension StickersViewController: UIScrollViewDelegate {
  165. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  166. let pageWidth = scrollView.bounds.width
  167. let pageFraction = scrollView.contentOffset.x / pageWidth
  168. self.pageControl.currentPage = Int(round(pageFraction))
  169. }
  170. }
  171. // MARK: - UICollectionViewDataSource
  172. extension StickersViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
  173. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  174. return stickers.count
  175. }
  176. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  177. stickersViewControllerDelegate?.didSelectImage(image: stickers[indexPath.item])
  178. }
  179. func numberOfSections(in collectionView: UICollectionView) -> Int {
  180. return 1
  181. }
  182. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  183. let identifier = "StickerCollectionViewCell"
  184. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! StickerCollectionViewCell
  185. cell.stickerImage.image = stickers[indexPath.item]
  186. return cell
  187. }
  188. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  189. return 4
  190. }
  191. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  192. return 0
  193. }
  194. }