Skip to content

Instantly share code, notes, and snippets.

@cozzin
Last active July 24, 2018 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cozzin/1abe6cb36e4bf8443d21d9cc3cdae946 to your computer and use it in GitHub Desktop.
Save cozzin/1abe6cb36e4bf8443d21d9cc3cdae946 to your computer and use it in GitHub Desktop.
//
// SideMenuViewController.swift
// sieum
//
// Created by 홍성호 on 2018. 6. 26..
// Copyright © 2018년 홍성호. All rights reserved.
//
import UIKit
import SnapKit
class SideMenuViewController: UIViewController {
private lazy var didUpdateViewConstraints: Bool = false
private var trailingConstraint: Constraint?
private var trailingMaxOffset: CGFloat {
return view.frame.width * 0.6
}
private var tableView: UITableView = {
let tableView = UITableView()
return tableView
}()
private var closeButton: UIButton = {
let closeButton = UIButton()
closeButton.backgroundColor = .black
closeButton.addTarget(self, action: #selector(onCloseButton(_:)), for: .touchUpInside)
closeButton.alpha = 0.0
return closeButton
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .clear
view.isOpaque = false
view.addSubview(tableView)
view.addSubview(closeButton)
tableView.snp.makeConstraints { make in
make.top.leading.bottom.equalToSuperview()
self.trailingConstraint = make.trailing.equalTo(view.snp.leading).constraint
}
closeButton.snp.makeConstraints { make in
make.top.trailing.bottom.equalToSuperview()
make.leading.equalTo(tableView.snp.trailing)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
show()
}
@objc func onCloseButton(_ sender: UIButton) {
hide { [weak self] in
self?.dismiss(animated: false)
}
}
private func show(completion: (() -> Void)? = nil) {
trailingConstraint?.update(offset: trailingMaxOffset)
animateLayout(animations: { [weak self] in
self?.closeButton.alpha = 0.5
}) { isFinished in
if isFinished {
completion?()
}
}
}
private func hide(completion: (() -> Void)? = nil) {
trailingConstraint?.update(offset: 0)
animateLayout(animations: { [weak self] in
self?.closeButton.alpha = 0.0
}) { isFinished in
if isFinished {
completion?()
}
}
}
}
extension UIViewController {
func animateLayout(duration: TimeInterval = 0.3, animations: (() -> Void)? = nil, completion: ((Bool) -> Void)? = nil) {
UIView.animate(withDuration: duration, animations: { [weak self] in
self?.view.layoutIfNeeded()
animations?()
}, completion: { isFinished in
completion?(isFinished)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment