Cómo usar el menú de la barra deslizante usando Swift en iOS sin un guión gráfico

Aquí está el código para el impresionante menú Blurry Slidebar sin usar el guión gráfico que escribo para mi proyecto.

Cree un nuevo archivo de clase de cacao touch de [math] UITableViewController [/ math] y asígnele cualquier nombre. Doy nombre [math] SideBarTableViewController [/ math].

Aquí está el código de mi SideBarTableViewController: –

//
// SideBarTableViewController.swift
// Technex18
//
// Creado por JOGENDRA el 25/03/17.
// Copyright © 2017 Jogendra Singh. Todos los derechos reservados.
//
importar UIKit
protocolo SideBarTableViewControllerDelegate {
func SideBarControlDidSelectRow (_ indexPath: IndexPath)
}
clase SideBarTableViewController: UITableViewController {
delegado var: SideBarTableViewControllerDelegate?
var tableData: Array = []
// MARK: – Fuente de datos de vista de tabla
anular func numberOfSections (en tableView: UITableView) -> Int {
volver 1
}
anular func tableView (_ tableView: UITableView, numberOfRowsInSection sección: Int) -> Int {
return tableData.count
}
anular func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
celda var: UITableViewCell? = tableView.dequeueReusableCell (withIdentifier: “cell”)
if cell == nil {
cell = UITableViewCell (estilo: UITableViewCellStyle.default, reuseIdentifier: “cell”)
// Configurar la celda …
cell! .backgroundColor = UIColor.clear
cell! .textLabel? .textColor = UIColor.darkText
let selectedView: UIView = UIView (frame: CGRect (x: 0, y: 0, width: cell! .frame.size.width, height: cell! .frame.size.height))
selectedView.backgroundColor = UIColor.black.withAlphaComponent (1.0)
cell? .textLabel? .highlightedTextColor = UIColor.white
cell! .selectedBackgroundView = selectedView;
// // agregando imágenes del lado izquierdo en la celda
// let itemImageView = UIImageView (frame: CGRect (x: 0, y: 0, width: 45, height: 45))
// let itemImage = UIImage (llamado: “arrow-up.png”)
// itemImageView.image = itemImage;
// cell? .addSubview (itemImageView);
}
cell! .textLabel? .text = tableData [indexPath.row]
volver celular!
}
anular func tableView (_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
retorno 45.0
}
anular func tableView (_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegar? .SideBarControlDidSelectRow (indexPath)
}
}

Y ahora cree una nueva clase de archivo táctil de cacao de [math] NSObject [/ math], y asígnele cualquier nombre. Yo uso [math] SideBar [/ math] como nombre.

Aquí está el código de mi archivo SideBar: –

//
// SideBar.swift
// Technex18
//
// Creado por JOGENDRA el 25/03/17.
// Copyright © 2017 Jogendra Singh. Todos los derechos reservados.
//
importar UIKit
@objc protocol SideBarDelegate {
func SideBarDidSelectButtonAtIndex (_ index: Int)
@objc func opcional SideBarWillClose ()
@objc func opcional SideBarWillOpen ()
}
clase SideBar: NSObject, SideBarTableViewControllerDelegate {
let barWidth: CGFloat = 150.0
let sideBarTableViewTopInset: CGFloat = 64.0
let sideBarContainerView: UIView = UIView ()
let sideBarTableViewController: SideBarTableViewController = SideBarTableViewController ()
var originView: UIView?
animador var: UIDynamicAnimator?
delegado var: SideBarDelegate?
var isSideBarOpen: Bool = false
anular init () {
super.init ()
}
init (sourceView: UIView, menuItems: Array ) {
super.init ()
originView = sourceView
sideBarTableViewController.tableData = menuItems
animator = UIDynamicAnimator (referenceView: originView!)
setupSideBar ()
deje showGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer (target: self, action: #selector (SideBar.handleSwipe (_ :)))
showGestureRecognizer.direction = UISwipeGestureRecognizerDirection.right
originView! .addGestureRecognizer (showGestureRecognizer)
let hideGestureRecognizer: UISwipeGestureRecognizer = UISwipeGestureRecognizer (target: self, action: #selector (SideBar.handleSwipe (_ :)))
hideGestureRecognizer.direction = UISwipeGestureRecognizerDirection.left
originView! .addGestureRecognizer (hideGestureRecognizer)
}
func setupSideBar () {
sideBarContainerView.frame = CGRect (x: -barWidth – 1, y: originView! .frame.origin.y, width: barWidth, height: originView! .frame.size.height)
sideBarContainerView.backgroundColor = UIColor.clear
sideBarContainerView.clipsToBounds = false
originView! .addSubview (sideBarContainerView)
let blurryView: UIVisualEffectView = UIVisualEffectView (efecto: UIBlurEffect (estilo: UIBlurEffectStyle.light))
blurryView.frame = sideBarContainerView.bounds
sideBarContainerView.addSubview (blurryView)
sideBarTableViewController.delegate = self
sideBarTableViewController.tableView.frame = sideBarContainerView.bounds
sideBarTableViewController.tableView.clipsToBounds = false
sideBarTableViewController.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
sideBarTableViewController.tableView.backgroundColor = UIColor.clear
sideBarTableViewController.tableView.scrollsToTop = false
sideBarTableViewController.tableView.contentInset = UIEdgeInsetsMake (sideBarTableViewTopInset, 0, 0, 0)
sideBarTableViewController.tableView.delegate = sideBarTableViewController
sideBarTableViewController.tableView.reloadData ()
sideBarContainerView.addSubview (sideBarTableViewController.tableView)
}
func handleSwipe (_ Recognizer: UISwipeGestureRecognizer) {
if Recognizer.direction == UISwipeGestureRecognizerDirection.left {
showSideBar (falso)
delegar? .SideBarWillClose? ()
} más {
showSideBar (verdadero)
}
}
func showSideBar (_ shouldOpen: Bool) {
animador! .removeAllBehaviors ()
isSideBarOpen = shouldOpen
dejar que gravityX: CGFloat = (shouldOpen)? 0.5: -0.5
dejar magnitud: CGFloat = (shouldOpen)? 20: -20
let boundaryX: CGFloat = (shouldOpen)? barWidth: -barWidth – 1
let gravityBehavior: UIGravityBehavior = UIGravityBehavior (elementos: [sideBarContainerView])
gravityBehavior.gravityDirection = CGVector (dx: gravityX, dy: 0)
animador! .addBehavior (gravityBehavior)
let collisionBehavior: UICollisionBehavior = UICollisionBehavior (elementos: [sideBarContainerView])
collisionBehavior.addBoundary (withIdentifier: “SideBarBoundary” como NSCopying, de: CGPoint (x: boundaryX, y: 20), a: CGPoint (x: boundaryX, y: originView! .frame.size.height))
animador! .addBehavior (collisionBehavior)
let pushBehavior: UIPushBehavior = UIPushBehavior (elementos: [sideBarContainerView], modo: UIPushBehaviorMode.instantaneous)
pushBehavior.magnitude = magnitud
animador! .addBehavior (pushBehavior)
deje SideBarBehavior: UIDynamicItemBehavior = UIDynamicItemBehavior (elementos: [sideBarContainerView])
SideBarBehavior.elasticity = 0.3
animador! .addBehavior (SideBarBehavior)
}
func SideBarControlDidSelectRow (_ indexPath: IndexPath) {
delegar? .SideBarDidSelectButtonAtIndex (indexPath.row)
}
}

Ahora puede usar su barra deslizante en cualquier página, simplemente agregando un elemento del menú.

agregue este código a su archivo [math] viewController [/ math] –

  1. Agregue [math] SideBarDelegate [/ math] a su clase viewController

2. Ahora agregue esta línea

barra lateral var: SideBar = SideBar ()

3. agregue esta línea en [math] override func viewDidLoad () [/ math] –

// lista del menú de la barra lateral
barra lateral = Barra lateral (sourceView: self.view, menuItems: [“ITEM1”, “ITEM2”, “ITEM3”, “ITEM4”, “ITEM5”, “ITEM6”, “ITEM7”, “ITEM8”,]);
sidebar.delegate = self;

4.Utilice esta función para la acción en su lista de menú: –

func SideBarDidSelectButtonAtIndex (_ index: Int) {
// tu código aquí
}

Espero que esto te ayudará. ¡Feliz codificación!