123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import UIKit
- import FloatingPanel
- class NCMenu: UITableViewController {
- var actions = [NCMenuAction]()
- static func makeNCMenu(with actions: [NCMenuAction]) -> NCMenu? {
- let menuViewController = UIStoryboard(name: "NCMenu", bundle: nil).instantiateInitialViewController() as? NCMenu
- menuViewController?.actions = actions
- return menuViewController
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- let menuAction = actions[indexPath.row]
- if let action = menuAction.action {
- self.dismiss(animated: true, completion: nil)
- action(menuAction)
- }
- }
-
- override func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return actions.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "menuActionCell", for: indexPath)
- cell.tintColor = NCBrandColor.shared.customer
- let action = actions[indexPath.row]
- let actionIconView = cell.viewWithTag(1) as? UIImageView
- let actionNameLabel = cell.viewWithTag(2) as? UILabel
- if action.action == nil {
- cell.selectionStyle = .none
- }
- if action.isOn {
- actionIconView?.image = action.onIcon
- actionNameLabel?.text = action.onTitle
- } else {
- actionIconView?.image = action.icon
- actionNameLabel?.text = action.title
- }
- cell.accessoryType = action.selectable && action.selected ? .checkmark : .none
- return cell
- }
-
- override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return 60
- }
- }
- extension NCMenu: FloatingPanelControllerDelegate {
- func floatingPanel(_ fpc: FloatingPanelController, layoutFor size: CGSize) -> FloatingPanelLayout {
- return NCMenuFloatingPanelLayout(numberOfActions: self.actions.count)
- }
- func floatingPanel(_ fpc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout {
- return NCMenuFloatingPanelLayout(numberOfActions: self.actions.count)
- }
- func floatingPanel(_ fpc: FloatingPanelController, animatorForDismissingWith velocity: CGVector) -> UIViewPropertyAnimator {
- return UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut)
- }
- func floatingPanel(_ fpc: FloatingPanelController, animatorForPresentingTo state: FloatingPanelState) -> UIViewPropertyAnimator {
- return UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut)
- }
- func floatingPanelWillEndDragging(_ fpc: FloatingPanelController, withVelocity velocity: CGPoint, targetState: UnsafeMutablePointer<FloatingPanelState>) {
- guard velocity.y > 750 else { return }
- fpc.dismiss(animated: true, completion: nil)
- }
- }
|