123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import UIKit
- open class SideViewController: UIViewController {
-
- public var contentView: UIView?
- override open func viewDidLoad() {
- super.viewDidLoad()
- view.isUserInteractionEnabled = true
- view.backgroundColor = UIColor(red: 52 / 255, green: 52 / 255, blue: 52 / 255, alpha: 0.7)
- showSide()
- }
-
- func showSide() {
- scrollLeft()
- colorGradientAppear()
- }
-
- @objc func hideSide() {
- scrollRight()
- colorGradientDisappear()
- }
- }
- extension SideViewController {
- func scrollLeft() {
- let animation = CATransition()
- animation.duration = 0.2
- animation.delegate = self
- animation.type = .moveIn
- animation.subtype = .fromLeft
-
- contentView?.layer.add(animation, forKey: nil)
- }
-
- func scrollRight() {
- let fromPosition = contentView!.layer.position
- let toPosition = CGPoint(x: -contentView!.frame.width, y: contentView!.frame.height / 2)
-
- let animation = CABasicAnimation(keyPath: "position")
- animation.duration = 0.2
- animation.delegate = self
- animation.isRemovedOnCompletion = false
- animation.fillMode = .forwards
- animation.toValue = NSValue(cgPoint: toPosition)
- animation.fromValue = NSValue(cgPoint: fromPosition)
-
- contentView?.layer.add(animation, forKey: "hide")
- }
-
- func colorGradientAppear() {
- let animation = CABasicAnimation(keyPath: "backgroundColor")
- animation.duration = 0.2
- animation.isRemovedOnCompletion = false
- animation.toValue = UIColor(red: 52 / 255,
- green: 52 / 255,
- blue: 52 / 255,
- alpha: 0.7).cgColor
- animation.fromValue = UIColor(red: 52 / 255,
- green: 52 / 255,
- blue: 52 / 255,
- alpha: 0).cgColor
-
- view.layer.add(animation, forKey: nil)
- }
-
- func colorGradientDisappear() {
- let animation = CABasicAnimation(keyPath: "backgroundColor")
- animation.duration = 0.2
- animation.isRemovedOnCompletion = false
- animation.toValue = UIColor(red: 52 / 255,
- green: 52 / 255,
- blue: 52 / 255,
- alpha: 0).cgColor
- animation.fromValue = UIColor(red: 52 / 255,
- green: 52 / 255,
- blue: 52 / 255,
- alpha: 0.7).cgColor
-
- view.layer.add(animation, forKey: nil)
- }
- }
- extension SideViewController: CAAnimationDelegate {
- func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
- if anim == contentView?.layer.animation(forKey: "hide") {
- view.removeFromSuperview()
- removeFromParent()
- return
- }
- configurationGestures()
- }
- }
- extension SideViewController {
- fileprivate func configurationGestures() {
- configurationTapGesture()
- configurationSwipeGesture()
- }
-
- fileprivate func configurationTapGesture() {
- let tap = UITapGestureRecognizer(target: self, action: #selector(hideSide))
- tap.delegate = self
- view.addGestureRecognizer(tap)
- }
-
- fileprivate func configurationSwipeGesture() {
- let swipe = UISwipeGestureRecognizer(target: self, action: #selector(hideSide))
- swipe.delegate = self
- swipe.direction = .left
- view.addGestureRecognizer(swipe)
- }
- }
- extension SideViewController: UIGestureRecognizerDelegate {
- func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
- if contentView?.frame.contains(touch.location(in: view)) ?? false { return false }
- return true
- }
- }
|