// // MyShopRightMenu.swift // xingchuangke // // Created by 李晓飞 on 2020/8/23. // Copyright © 2020 Virgil. All rights reserved. // import UIKit class XF_RightMenu: UIView { let duration: TimeInterval = 0.5 let contentWidth: CGFloat = 280 var contentView: UIView = UIView.init() { didSet { addContainer() } } var bgView: UIView = { let v = UIView.init() v.backgroundColor = UIColor.black v.alpha = 0.5 return v }() var containerView: UIView = { let v = UIView.init() v.backgroundColor = UIColor.white return v }() override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func setup() { bgView.frame = self.frame let w = self.frame.width containerView.frame = CGRect.init(x: w, y: 0, width: contentWidth, height: self.frame.height) self.addSubview(bgView) self.addSubview(containerView) let tapG = UITapGestureRecognizer.init(target: self, action: #selector(tapGesture)) bgView.addGestureRecognizer(tapG) self.isHidden = true } func addContainer() { self.contentView.frame = self.containerView.bounds self.containerView.addSubview(contentView) } // MARK: - show/hidden view func showView() { self.isHidden = false UIView.animate(withDuration: duration, animations: { let w = self.frame.width self.containerView.frame = CGRect.init(x: w - self.contentWidth, y: 0, width: self.contentWidth, height: self.frame.height) }) { (_) in } } func hiddenView() { UIView.animate(withDuration: duration, animations: { let w = self.frame.width self.containerView.frame = CGRect.init(x: w, y: 0, width: self.contentWidth, height: self.frame.height) }) { (_) in self.isHidden = true } } // MARK: - action @objc func tapGesture() { hiddenView() } /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ }