TLAlbumPopView.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // TLAlbumPopView.swift
  3. // TLPhotosPicker
  4. //
  5. // Created by wade.hawk on 2017. 4. 19..
  6. // Copyright © 2017년 wade.hawk. All rights reserved.
  7. //
  8. import UIKit
  9. protocol PopupViewProtocol: class {
  10. var bgView: UIView! { get set }
  11. var popupView: UIView! { get set }
  12. var originalFrame: CGRect { get set }
  13. var show: Bool { get set }
  14. func setupPopupFrame()
  15. }
  16. extension PopupViewProtocol where Self: UIView {
  17. fileprivate func getFrame(scale: CGFloat) -> CGRect {
  18. var frame = self.originalFrame
  19. frame.size.width = frame.size.width * scale
  20. frame.size.height = frame.size.height * scale
  21. frame.origin.x = self.frame.width/2 - frame.width/2
  22. return frame
  23. }
  24. func setupPopupFrame() {
  25. if self.originalFrame != self.popupView.frame {
  26. self.originalFrame = self.popupView.frame
  27. }
  28. }
  29. func show(_ show: Bool, duration: TimeInterval = 0.1) {
  30. guard self.show != show else { return }
  31. self.layer.removeAllAnimations()
  32. self.isHidden = false
  33. self.popupView.frame = show ? getFrame(scale: 0.1) : self.popupView.frame
  34. self.bgView.alpha = show ? 0 : 1
  35. UIView.animate(withDuration: duration, animations: {
  36. self.bgView.alpha = show ? 1 : 0
  37. self.popupView.transform = show ? CGAffineTransform(scaleX: 1.05, y: 1.05) : CGAffineTransform(scaleX: 0.1, y: 0.1)
  38. self.popupView.frame = show ? self.getFrame(scale: 1.05) : self.getFrame(scale: 0.1)
  39. }) { _ in
  40. self.isHidden = show ? false : true
  41. UIView.animate(withDuration: duration) {
  42. if show {
  43. self.popupView.transform = CGAffineTransform(scaleX: 1, y: 1)
  44. self.popupView.frame = self.originalFrame
  45. }
  46. self.show = show
  47. }
  48. }
  49. }
  50. }
  51. open class TLAlbumPopView: UIView,PopupViewProtocol {
  52. @IBOutlet var bgView: UIView!
  53. @IBOutlet var popupView: UIView!
  54. @IBOutlet var popupViewHeight: NSLayoutConstraint!
  55. @IBOutlet var tableView: UITableView!
  56. @objc var originalFrame = CGRect.zero
  57. @objc var show = false
  58. deinit {
  59. // print("deinit TLAlbumPopView")
  60. }
  61. override open func awakeFromNib() {
  62. super.awakeFromNib()
  63. self.popupView.layer.cornerRadius = 5.0
  64. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapBgView))
  65. self.bgView.addGestureRecognizer(tapGesture)
  66. self.tableView.register(UINib(nibName: "TLCollectionTableViewCell", bundle: Bundle(for: TLCollectionTableViewCell.self)), forCellReuseIdentifier: "TLCollectionTableViewCell")
  67. }
  68. @objc func tapBgView() {
  69. self.show(false)
  70. }
  71. }