暫無描述

SideViewController.swift 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // SideViewController.swift
  3. // PaiAi
  4. //
  5. // Created by ffib on 2018/12/12.
  6. // Copyright © 2018 yb. All rights reserved.
  7. //
  8. import UIKit
  9. open class SideViewController: UIViewController {
  10. public var contentView: UIView?
  11. override open func viewDidLoad() {
  12. super.viewDidLoad()
  13. view.isUserInteractionEnabled = true
  14. view.backgroundColor = UIColor(red: 52 / 255, green: 52 / 255, blue: 52 / 255, alpha: 0.7)
  15. showSide()
  16. }
  17. func showSide() {
  18. scrollLeft()
  19. colorGradientAppear()
  20. }
  21. @objc func hideSide() {
  22. scrollRight()
  23. colorGradientDisappear()
  24. }
  25. }
  26. //animation
  27. extension SideViewController {
  28. func scrollLeft() {
  29. let animation = CATransition()
  30. animation.duration = 0.2
  31. animation.delegate = self
  32. animation.type = .moveIn
  33. animation.subtype = .fromLeft
  34. contentView?.layer.add(animation, forKey: nil)
  35. }
  36. func scrollRight() {
  37. let fromPosition = contentView!.layer.position
  38. let toPosition = CGPoint(x: -contentView!.frame.width, y: contentView!.frame.height / 2)
  39. let animation = CABasicAnimation(keyPath: "position")
  40. animation.duration = 0.2
  41. animation.delegate = self
  42. animation.isRemovedOnCompletion = false
  43. animation.fillMode = .forwards
  44. animation.toValue = NSValue(cgPoint: toPosition)
  45. animation.fromValue = NSValue(cgPoint: fromPosition)
  46. contentView?.layer.add(animation, forKey: "hide")
  47. }
  48. func colorGradientAppear() {
  49. let animation = CABasicAnimation(keyPath: "backgroundColor")
  50. animation.duration = 0.2
  51. animation.isRemovedOnCompletion = false
  52. animation.toValue = UIColor(red: 52 / 255,
  53. green: 52 / 255,
  54. blue: 52 / 255,
  55. alpha: 0.7).cgColor
  56. animation.fromValue = UIColor(red: 52 / 255,
  57. green: 52 / 255,
  58. blue: 52 / 255,
  59. alpha: 0).cgColor
  60. view.layer.add(animation, forKey: nil)
  61. }
  62. func colorGradientDisappear() {
  63. let animation = CABasicAnimation(keyPath: "backgroundColor")
  64. animation.duration = 0.2
  65. animation.isRemovedOnCompletion = false
  66. animation.toValue = UIColor(red: 52 / 255,
  67. green: 52 / 255,
  68. blue: 52 / 255,
  69. alpha: 0).cgColor
  70. animation.fromValue = UIColor(red: 52 / 255,
  71. green: 52 / 255,
  72. blue: 52 / 255,
  73. alpha: 0.7).cgColor
  74. view.layer.add(animation, forKey: nil)
  75. }
  76. }
  77. extension SideViewController: CAAnimationDelegate {
  78. func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
  79. if anim == contentView?.layer.animation(forKey: "hide") {
  80. view.removeFromSuperview()
  81. removeFromParent()
  82. return
  83. }
  84. configurationGestures()
  85. }
  86. }
  87. //gesture
  88. extension SideViewController {
  89. fileprivate func configurationGestures() {
  90. configurationTapGesture()
  91. configurationSwipeGesture()
  92. }
  93. fileprivate func configurationTapGesture() {
  94. let tap = UITapGestureRecognizer(target: self, action: #selector(hideSide))
  95. tap.delegate = self
  96. view.addGestureRecognizer(tap)
  97. }
  98. fileprivate func configurationSwipeGesture() {
  99. let swipe = UISwipeGestureRecognizer(target: self, action: #selector(hideSide))
  100. swipe.delegate = self
  101. swipe.direction = .left
  102. view.addGestureRecognizer(swipe)
  103. }
  104. }
  105. extension SideViewController: UIGestureRecognizerDelegate {
  106. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  107. if contentView?.frame.contains(touch.location(in: view)) ?? false { return false }
  108. return true
  109. }
  110. }