IHPresentBottom.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // IHPresentBottom.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/1/7.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class IHPresentBottom: UIPresentationController {
  10. var controllerHeight : CGFloat? = 0
  11. lazy var backView: UIView = {
  12. let backView = UIView(frame: self.containerView!.bounds)
  13. backView.backgroundColor = UIColor.black.withAlphaComponent(0.4)
  14. let tap = UITapGestureRecognizer(target: self, action: #selector(onclick(_:)))
  15. tap.numberOfTapsRequired = 1
  16. tap.numberOfTouchesRequired = 1
  17. backView.addGestureRecognizer(tap)
  18. return backView
  19. }()
  20. override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
  21. super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
  22. controllerHeight = presentedViewController.controllerHeight == 0 ? KSCREENHEIGHT : presentedViewController.controllerHeight
  23. }
  24. override func presentationTransitionWillBegin() {
  25. super.presentationTransitionWillBegin()
  26. self.backView.alpha = 0
  27. self.containerView?.addSubview(self.backView)
  28. UIView.animate(withDuration: 1.0) {
  29. self.backView.alpha = 1
  30. }
  31. }
  32. override func dismissalTransitionWillBegin() {
  33. super.dismissalTransitionWillBegin()
  34. UIView.animate(withDuration: 1.0) {
  35. self.backView.alpha = 0
  36. }
  37. }
  38. override func dismissalTransitionDidEnd(_ completed: Bool) {
  39. super.dismissalTransitionDidEnd(completed)
  40. if completed == true {
  41. self.backView.removeFromSuperview()
  42. }
  43. }
  44. override var frameOfPresentedViewInContainerView: CGRect{
  45. return CGRect(x: 0, y: KSCREENHEIGHT - controllerHeight!, width: KSCREENWIDTH, height: controllerHeight!)
  46. }
  47. @objc func onclick(_ gestureRecognzier:UIGestureRecognizer) {
  48. let point = gestureRecognzier.location(in: self.backView)
  49. let frame = self.frameOfPresentedViewInContainerView
  50. if frame.contains(point) == false {
  51. self.presentedViewController .dismiss(animated: true, completion: nil)
  52. }
  53. }
  54. }