|
//
// SideViewController.swift
// PaiAi
//
// Created by ffib on 2018/12/12.
// Copyright © 2018 yb. All rights reserved.
//
import UIKit
class SideViewController: UIViewController {
var contentView: UIView?
override 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()
}
}
//animation
extension SideViewController {
func scrollLeft() {
let animation = CATransition()
animation.duration = 0.2
animation.delegate = self
animation.type = kCATransitionMoveIn
animation.subtype = kCATransitionFromLeft
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 = kCAFillModeForwards
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()
removeFromParentViewController()
return
}
configurationGestures()
}
}
//gesture
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
}
}
|