- Finalised The side menu integration in the project.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// TransitionAnimator.swift
|
||||
// SideMenu
|
||||
//
|
||||
// Created by kukushi on 2018/8/8.
|
||||
// Copyright © 2018 kukushi. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
// A Simple transition animator can be configured with animation options.
|
||||
public class BasicTransitionAnimator: NSObject, UIViewControllerAnimatedTransitioning {
|
||||
let animationOptions: UIView.AnimationOptions
|
||||
let duration: TimeInterval
|
||||
|
||||
/// Initialize a new animator with animation options and duration.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - options: animation options
|
||||
/// - duration: animation duration
|
||||
public init(options: UIView.AnimationOptions = .transitionCrossDissolve, duration: TimeInterval = 0.4) {
|
||||
self.animationOptions = options
|
||||
self.duration = duration
|
||||
}
|
||||
|
||||
// MARK: UIViewControllerAnimatedTransitioning
|
||||
|
||||
public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
|
||||
return duration
|
||||
}
|
||||
|
||||
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
|
||||
guard let fromViewController = transitionContext.viewController(forKey: .from),
|
||||
let toViewController = transitionContext.viewController(forKey: .to) else {
|
||||
return
|
||||
}
|
||||
|
||||
transitionContext.containerView.addSubview(toViewController.view)
|
||||
|
||||
let duration = transitionDuration(using: transitionContext)
|
||||
|
||||
UIView.transition(from: fromViewController.view,
|
||||
to: toViewController.view,
|
||||
duration: duration,
|
||||
options: animationOptions,
|
||||
completion: { (_) in
|
||||
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
|
||||
})
|
||||
}
|
||||
}
|
||||
103
WOKA/TabBar & SideMenu/SideMenu/Delegate.swift
Normal file
103
WOKA/TabBar & SideMenu/SideMenu/Delegate.swift
Normal file
@@ -0,0 +1,103 @@
|
||||
//
|
||||
// SideMenuControllerDelegate.swift
|
||||
// SideMenu
|
||||
//
|
||||
// Created by kukushi on 2018/8/8.
|
||||
// Copyright © 2018 kukushi. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
// Delegate Methods
|
||||
public protocol SideMenuControllerDelegate: AnyObject {
|
||||
|
||||
// MARK: Animation
|
||||
|
||||
/// Called to allow the delegate to return a non-interactive animator object for use during view controller transitions.
|
||||
/// Same with UIKit's ``navigationController(_:animationControllerFor:from:to:)``.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - sideMenuController: The side menu controller
|
||||
/// - fromVC: The currently visible view controller.
|
||||
/// - toVC: The view controller that should be visible at the end of the transition.
|
||||
/// - Returns: The animator object responsible for managing the transition animations,
|
||||
/// or nil if you want to use the fade transitions.
|
||||
func sideMenuController(_ sideMenuController: SideMenuController,
|
||||
animationControllerFrom fromVC: UIViewController,
|
||||
to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
|
||||
|
||||
// MARK: Switching
|
||||
|
||||
/// Side menu will show a view controller.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - sideMenuController: current side menu controller
|
||||
/// - viewController: the view controller to show
|
||||
/// - animated: whether it's animated
|
||||
func sideMenuController(_ sideMenuController: SideMenuController, willShow viewController: UIViewController, animated: Bool)
|
||||
|
||||
/// Side menu did showed a view controller.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - sideMenuController: current side menu controller
|
||||
/// - viewController: the view controller shown
|
||||
/// - animated: whether it's animated
|
||||
func sideMenuController(_ sideMenuController: SideMenuController, didShow viewController: UIViewController, animated: Bool)
|
||||
|
||||
// MARK: Revealing
|
||||
|
||||
/// Asks the delegate whether the side menu should be shown.
|
||||
///
|
||||
/// Triggered by the pan gesture.
|
||||
/// - Parameter sideMenuController: The side menu
|
||||
/// - Returns: Whether the menu should be revealed.
|
||||
func sideMenuControllerShouldRevealMenu(_ sideMenuController: SideMenuController) -> Bool
|
||||
|
||||
/// Side menu is going to reveal.
|
||||
///
|
||||
/// - Parameter sideMenu: The side menu
|
||||
func sideMenuControllerWillRevealMenu(_ sideMenuController: SideMenuController)
|
||||
|
||||
/// Side menu did revealed.
|
||||
///
|
||||
/// - Parameter sideMenu: The side menu
|
||||
func sideMenuControllerDidRevealMenu(_ sideMenuController: SideMenuController)
|
||||
|
||||
/// Side menu is going to hide.
|
||||
///
|
||||
/// - Parameter sideMenu: The side menu
|
||||
func sideMenuControllerWillHideMenu(_ sideMenuController: SideMenuController)
|
||||
|
||||
/// Side menu did hided.
|
||||
///
|
||||
/// - Parameter sideMenu: The side menu
|
||||
func sideMenuControllerDidHideMenu(_ sideMenuController: SideMenuController)
|
||||
|
||||
|
||||
/// Get the width of side menu in current size
|
||||
/// - Parameter sideMenuController: The side menu
|
||||
func sideMenuControllerGetMenuWidth(_ sideMenuController: SideMenuController, for size: CGSize) -> CGFloat?
|
||||
}
|
||||
|
||||
// Provides default implementation for delegates
|
||||
public extension SideMenuControllerDelegate {
|
||||
func sideMenuController(_ sideMenuController: SideMenuController,
|
||||
animationControllerFrom fromVC: UIViewController,
|
||||
to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
|
||||
return nil
|
||||
}
|
||||
|
||||
func sideMenuController(_ sideMenuController: SideMenuController,
|
||||
willShow viewController: UIViewController,
|
||||
animated: Bool) {}
|
||||
func sideMenuController(_ sideMenuController: SideMenuController,
|
||||
didShow viewController: UIViewController,
|
||||
animated: Bool) {}
|
||||
func sideMenuControllerShouldRevealMenu(_ sideMenuController: SideMenuController) -> Bool { true }
|
||||
func sideMenuControllerWillRevealMenu(_ sideMenuController: SideMenuController) {}
|
||||
func sideMenuControllerDidRevealMenu(_ sideMenuController: SideMenuController) {}
|
||||
func sideMenuControllerWillHideMenu(_ sideMenuController: SideMenuController) {}
|
||||
func sideMenuControllerDidHideMenu(_ sideMenuController: SideMenuController) {}
|
||||
func sideMenuControllerGetMenuWidth(_ sideMenuController: SideMenuController, for size: CGSize) -> CGFloat? { nil }
|
||||
}
|
||||
137
WOKA/TabBar & SideMenu/SideMenu/Preferences.swift
Normal file
137
WOKA/TabBar & SideMenu/SideMenu/Preferences.swift
Normal file
@@ -0,0 +1,137 @@
|
||||
//
|
||||
// SideMenuPreferences.swift
|
||||
// SideMenu
|
||||
//
|
||||
// Created by kukushi on 21/02/2018.
|
||||
// Copyright © 2018 kukushi. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension SideMenuController {
|
||||
/// The preferences of side menu controller.
|
||||
public struct Preferences {
|
||||
|
||||
/// The animation that will apply to the status bar when the menu is revealed/hidden.
|
||||
@available(iOS, deprecated: 13.0, message: "Status bar animation no longer work after iOS 13")
|
||||
public enum StatusBarBehavior {
|
||||
/// Nothing will happen to the status bar.
|
||||
case none
|
||||
/// The status bar will slide up when revealed and slide down when hidden.
|
||||
case slide
|
||||
/// The status bar will fade out when revealed and show up when hidden.
|
||||
case fade
|
||||
/// The status bar on the side menu will be hidden (without animation),
|
||||
/// while the one the on content view will still show.
|
||||
case hideOnMenu
|
||||
}
|
||||
|
||||
/// The direction where menu will show up from.
|
||||
public enum MenuDirection {
|
||||
/// Side menu will reveal from the left side.
|
||||
case left
|
||||
/// Side menu will reveal from the right side.
|
||||
case right
|
||||
}
|
||||
|
||||
/// The menu view position compared to the content view.
|
||||
public enum MenuPosition {
|
||||
/// Menu view is placed above the content view.
|
||||
case above
|
||||
/// Menu view is placed below the content view.
|
||||
case under
|
||||
/// Menu view is placed in the same layer with the content view.
|
||||
case sideBySide
|
||||
}
|
||||
|
||||
public struct Animation {
|
||||
/// The animation interval of revealing side menu. Default is `0.4`.
|
||||
public var revealDuration: TimeInterval = 0.4
|
||||
|
||||
/// The animation interval of hiding side menu. Default is `0.4`.
|
||||
public var hideDuration: TimeInterval = 0.4
|
||||
|
||||
/// The animation option of reveal/hide. Default is ``.curveEaseInOut``.
|
||||
public var options: UIView.AnimationOptions = .curveEaseInOut
|
||||
|
||||
/// The amping ratio option used in the revealing and hiding animation of the menu. The default is `1`.
|
||||
public var dampingRatio: CGFloat = 1
|
||||
|
||||
/// The ``initialSpringVelocity`` option used in the revealing and hiding animation of the menu. The default is `1`.
|
||||
public var initialSpringVelocity: CGFloat = 1
|
||||
|
||||
/// Whether a shadow effect should be added on content view when revealing the menu. The default is true.
|
||||
/// If the position is `.under`, the shadow effect will not be added even if this value is set to `true`.
|
||||
public var shouldAddShadowWhenRevealing = true
|
||||
|
||||
/// The shadow's alpha when showing on the content view. Default is `0.2`.
|
||||
public var shadowAlpha: CGFloat = 0.2
|
||||
|
||||
/// The shadow's color when showing on the content view. Default is `black`.
|
||||
public var shadowColor: UIColor = .black
|
||||
|
||||
/// Whether we should add a blurr effect on shadow when revealing
|
||||
public var shouldAddBlurWhenRevealing = false
|
||||
}
|
||||
|
||||
public struct Configuration {
|
||||
/// The width of the side menu. The default is `300`.
|
||||
/// Note that you should only modify this property before the side menu controller is initialized.
|
||||
public var menuWidth: CGFloat = 300
|
||||
|
||||
/// The position of the side menu. Default is ``.above``.
|
||||
/// Note that you should only modify this property before the side menu controller is initialized.
|
||||
public var position: MenuPosition = .above
|
||||
|
||||
/// Whether the direction of side menu should be reversed when the user interaction layout direction is RTL.
|
||||
/// More specific, when the app is using a right to left (RTL) language, the direction of side menu will be
|
||||
/// reversed
|
||||
public var shouldRespectLanguageDirection = true
|
||||
|
||||
// Whether the direction of side menu should be reversed forcefully to RTL. This is required if we're changing the application language in runtime.
|
||||
// The direction of side menu will be reversed forcefully. The default is false.
|
||||
public var forceRightToLeft = false
|
||||
|
||||
/// The direction of side menu. Default is ``.left``.
|
||||
/// Note that you should only modify this property before the side menu controller is initialized.
|
||||
public var direction: MenuDirection = .left
|
||||
|
||||
/// The status bar behavior when menu revealed / hidden. Default is `.none`.
|
||||
@available(iOS, deprecated: 13.0, message: "Status bar animation no longer work after iOS 13")
|
||||
public var statusBarBehavior: StatusBarBehavior = .none
|
||||
|
||||
/// Whether the pan gesture should be enabled. The default is true.
|
||||
public var enablePanGesture = true
|
||||
|
||||
/// If enabled, the menu view will act like a rubber band when reaching the border. The default is true.
|
||||
public var enableRubberEffectWhenPanning = true
|
||||
|
||||
/// If enabled, the menu view will be hidden when the app entering background. The default is false.
|
||||
public var hideMenuWhenEnteringBackground = false
|
||||
|
||||
/// The cache key for the first content view controller.
|
||||
public var defaultCacheKey: String?
|
||||
|
||||
/// The side menu should use content's supported orientations. Default is false.
|
||||
public var shouldUseContentSupportedOrientations: Bool = false
|
||||
|
||||
/// The supported orientations of side menu controller. Default is ``.allButUpsideDown``.
|
||||
public var supportedOrientations: UIInterfaceOrientationMask = .allButUpsideDown
|
||||
|
||||
/// The side menu shouldAutorotate. Default is `true`.
|
||||
public var shouldAutorotate: Bool = true
|
||||
|
||||
/// The sensitivity of the pan gesture recognizer revealing menu view controller.
|
||||
public var panGestureSensitivity: CGFloat = 0.25
|
||||
|
||||
/// If the side menu should keep open on rotation. Default is `false`.
|
||||
public var keepsMenuOpenAfterRotation: Bool = false
|
||||
}
|
||||
|
||||
/// The basic configuration of side menu.
|
||||
public var basic = Configuration()
|
||||
|
||||
/// The animation configuration of side menu.
|
||||
public var animation = Animation()
|
||||
}
|
||||
}
|
||||
40
WOKA/TabBar & SideMenu/SideMenu/Segue.swift
Normal file
40
WOKA/TabBar & SideMenu/SideMenu/Segue.swift
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// SideMenuSegue.swift
|
||||
// SideMenu
|
||||
//
|
||||
// Created by kukushi on 2018/8/8.
|
||||
// Copyright © 2018 kukushi. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
/// Custom Segue that is required for ``SideMenuController`` to be used in Storyboard.
|
||||
open class SideMenuSegue: UIStoryboardSegue {
|
||||
|
||||
/// The type of segue
|
||||
public enum ContentType: String {
|
||||
/// represent the content scene of side menu
|
||||
case content = "SideMenu.Content"
|
||||
/// represent the menu scene of side menu
|
||||
case menu = "SideMenu.Menu"
|
||||
}
|
||||
|
||||
/// current content type
|
||||
public var contentType = ContentType.content
|
||||
|
||||
/// Performing the segue, will change the corresponding view controller of side menu to `destination` view controller.
|
||||
/// This method is called when loading from storyboard.
|
||||
open override func perform() {
|
||||
guard let sideMenuController = source as? SideMenuController else {
|
||||
return
|
||||
}
|
||||
|
||||
switch contentType {
|
||||
case .content:
|
||||
sideMenuController.contentViewController = destination
|
||||
case .menu:
|
||||
sideMenuController.menuViewController = destination
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
16
WOKA/TabBar & SideMenu/SideMenu/SideMenu.h
Normal file
16
WOKA/TabBar & SideMenu/SideMenu/SideMenu.h
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// SideMenu.h
|
||||
// SideMenu
|
||||
//
|
||||
// Created by kukushi on 03/02/2018.
|
||||
// Copyright © 2018 kukushi. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
//! Project version number for SideMenu.
|
||||
FOUNDATION_EXPORT double SideMenuVersionNumber;
|
||||
|
||||
//! Project version string for SideMenu.
|
||||
FOUNDATION_EXPORT const unsigned char SideMenuVersionString[];
|
||||
|
||||
855
WOKA/TabBar & SideMenu/SideMenu/SideMenuController.swift
Normal file
855
WOKA/TabBar & SideMenu/SideMenu/SideMenuController.swift
Normal file
@@ -0,0 +1,855 @@
|
||||
//
|
||||
// SideMenuController.swift
|
||||
// SideMenu
|
||||
//
|
||||
// Created by kukushi on 10/02/2018.
|
||||
// Copyright © 2018 kukushi. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
// MARK: SideMenuController
|
||||
|
||||
/// A container view controller owns a menu view controller and a content view controller.
|
||||
///
|
||||
/// The overall architecture of SideMenuController is:
|
||||
///
|
||||
/// SideMenuController
|
||||
///
|
||||
/// ├── Menu View Controller
|
||||
///
|
||||
/// └── Content View Controller
|
||||
open class SideMenuController: UIViewController {
|
||||
|
||||
/// Configure this property to change the behavior of SideMenuController;
|
||||
public static var preferences = Preferences()
|
||||
private var preferences: Preferences {
|
||||
Self.preferences
|
||||
}
|
||||
|
||||
private lazy var adjustedDirection = Preferences.MenuDirection.left
|
||||
|
||||
private var isInitiatedFromStoryboard: Bool {
|
||||
storyboard != nil
|
||||
}
|
||||
|
||||
private var menuWidth: CGFloat {
|
||||
delegate?.sideMenuControllerGetMenuWidth(self, for: view.frame.size) ?? preferences.basic.menuWidth
|
||||
}
|
||||
|
||||
/// The identifier of content view controller segue.
|
||||
/// If the SideMenuController instance is initiated from IB, this identifier will
|
||||
/// be used to retrieve the content view controller.
|
||||
@IBInspectable public var contentSegueID: String = SideMenuSegue.ContentType.content.rawValue
|
||||
|
||||
/// The identifier of menu view controller segue.
|
||||
/// If the SideMenuController instance is initiated from IB, this identifier will
|
||||
/// be used to retrieve the menu view controller.
|
||||
@IBInspectable public var menuSegueID: String = SideMenuSegue.ContentType.menu.rawValue
|
||||
|
||||
/// Caching
|
||||
private lazy var lazyCachedViewControllerGenerators: [String: () -> UIViewController?] = [:]
|
||||
private lazy var lazyCachedViewControllers: [String: UIViewController] = [:]
|
||||
|
||||
/// The side menu controller's delegate object.
|
||||
public weak var delegate: SideMenuControllerDelegate?
|
||||
|
||||
// swiftlint:disable:next weak_delegate
|
||||
/// Tell whether ``contentViewController`` setter should call the delegate.
|
||||
/// Work as a workaround when switching content view controller from other animation approach which also change the
|
||||
private var shouldCallSwitchingDelegate = true
|
||||
|
||||
// swiftlint:disable:next implicitly_unwrapped_optional
|
||||
/// The content view controller. Changes its value will change the display immediately.
|
||||
/// If the new value is already one of the side menu controller's child controllers, nothing will happen beside value change.
|
||||
/// If you want a caching approach, use ``setContentViewController(with:animated:completion:)``. Its value should not be nil.
|
||||
open var contentViewController: UIViewController! {
|
||||
didSet {
|
||||
guard contentViewController !== oldValue &&
|
||||
isViewLoaded &&
|
||||
!children.contains(contentViewController) else {
|
||||
return
|
||||
}
|
||||
|
||||
if shouldCallSwitchingDelegate {
|
||||
delegate?.sideMenuController(self, willShow: contentViewController, animated: false)
|
||||
}
|
||||
|
||||
load(contentViewController, on: contentContainerView)
|
||||
contentContainerView.sendSubviewToBack(contentViewController.view)
|
||||
unload(oldValue)
|
||||
|
||||
if shouldCallSwitchingDelegate {
|
||||
delegate?.sideMenuController(self, didShow: contentViewController, animated: false)
|
||||
}
|
||||
|
||||
setNeedsStatusBarAppearanceUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
// swiftlint:disable:next implicitly_unwrapped_optional
|
||||
/// The menu view controller. Its value should not be nil.
|
||||
open var menuViewController: UIViewController! {
|
||||
didSet {
|
||||
guard menuViewController !== oldValue && isViewLoaded else {
|
||||
return
|
||||
}
|
||||
|
||||
load(menuViewController, on: menuContainerView)
|
||||
unload(oldValue)
|
||||
}
|
||||
}
|
||||
|
||||
private let menuContainerView = UIView()
|
||||
private let contentContainerView = UIView()
|
||||
private var statusBarScreenShotView: UIView?
|
||||
|
||||
/// Return true if the menu is now revealing.
|
||||
open var isMenuRevealed = false
|
||||
|
||||
private var shouldShowShadowOnContent: Bool {
|
||||
return preferences.animation.shouldAddShadowWhenRevealing && preferences.basic.position != .under
|
||||
}
|
||||
|
||||
/// States used in panning gesture
|
||||
private var isValidatePanningBegan = false
|
||||
private var panningBeganPointX: CGFloat = 0
|
||||
|
||||
private var isContentOrMenuNotInitialized: Bool {
|
||||
return menuViewController == nil || contentViewController == nil
|
||||
}
|
||||
|
||||
/// The view responsible for tapping to hide the menu and shadow
|
||||
private weak var contentContainerOverlay: UIView?
|
||||
|
||||
// The pan gesture recognizer responsible for revealing and hiding side menu
|
||||
private weak var panGestureRecognizer: UIPanGestureRecognizer?
|
||||
|
||||
var shouldReverseDirection: Bool {
|
||||
if preferences.basic.forceRightToLeft { return true }
|
||||
guard preferences.basic.shouldRespectLanguageDirection else {
|
||||
return false
|
||||
}
|
||||
let attribute = view.semanticContentAttribute
|
||||
let layoutDirection = UIView.userInterfaceLayoutDirection(for: attribute)
|
||||
return layoutDirection == .rightToLeft
|
||||
}
|
||||
|
||||
// MARK: Initialization
|
||||
|
||||
/// Creates a ``SideMenuController`` instance with the content view controller and menu view controller.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - contentViewController: the content view controller
|
||||
/// - menuViewController: the menu view controller
|
||||
public convenience init(contentViewController: UIViewController, menuViewController: UIViewController) {
|
||||
self.init(nibName: nil, bundle: nil)
|
||||
|
||||
// Assignment in initializer won't trigger the setter
|
||||
self.contentViewController = contentViewController
|
||||
self.menuViewController = menuViewController
|
||||
}
|
||||
|
||||
deinit {
|
||||
unregisterNotifications()
|
||||
}
|
||||
|
||||
// MARK: Life Cycle
|
||||
|
||||
/// ``SideMenuController`` may be initialized from Storyboard, thus we shouldn't load the view in `loadView()`.
|
||||
/// As mentioned by Apple, "If you use Interface Builder to create your views and initialize the view controller,
|
||||
/// you must not override this method."
|
||||
open override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
// Setup from the IB
|
||||
// Side menu may be initialized from the IB while segues are not used, thus passing the performing of
|
||||
// segues if content and menu is already set
|
||||
if isInitiatedFromStoryboard && isContentOrMenuNotInitialized {
|
||||
// Note that if you are using the `SideMenuController` from the IB, you must supply the default or
|
||||
// custom view controller ID in the storyboard.
|
||||
performSegue(withIdentifier: contentSegueID, sender: self)
|
||||
performSegue(withIdentifier: menuSegueID, sender: self)
|
||||
}
|
||||
|
||||
if isContentOrMenuNotInitialized {
|
||||
fatalError("[SideMenuSwift] `menuViewController` or `contentViewController` should not be nil.")
|
||||
}
|
||||
|
||||
contentContainerView.frame = view.bounds
|
||||
view.addSubview(contentContainerView)
|
||||
|
||||
resolveDirection(with: contentContainerView)
|
||||
|
||||
menuContainerView.frame = sideMenuFrame(visibility: false)
|
||||
view.addSubview(menuContainerView)
|
||||
|
||||
load(contentViewController, on: contentContainerView)
|
||||
load(menuViewController, on: menuContainerView)
|
||||
|
||||
if preferences.basic.position == .under {
|
||||
view.bringSubviewToFront(contentContainerView)
|
||||
}
|
||||
|
||||
// Forwarding status bar style/hidden status to content view controller
|
||||
setNeedsStatusBarAppearanceUpdate()
|
||||
|
||||
if let key = preferences.basic.defaultCacheKey {
|
||||
lazyCachedViewControllers[key] = contentViewController
|
||||
}
|
||||
|
||||
configureGesturesRecognizer()
|
||||
setUpNotifications()
|
||||
|
||||
// sideMenuController?.cache(viewController: SideMenuVC(), with: "second")
|
||||
// sideMenuController?.cache(viewController: TabBarVC(), with: "second")
|
||||
|
||||
}
|
||||
|
||||
private func resolveDirection(with view: UIView) {
|
||||
if shouldReverseDirection {
|
||||
adjustedDirection = (preferences.basic.direction == .left ? .right : .left)
|
||||
} else {
|
||||
adjustedDirection = preferences.basic.direction
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Storyboard
|
||||
|
||||
open override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
||||
guard let segue = segue as? SideMenuSegue, let identifier = segue.identifier else {
|
||||
return
|
||||
}
|
||||
switch identifier {
|
||||
case contentSegueID:
|
||||
segue.contentType = .content
|
||||
case menuSegueID:
|
||||
segue.contentType = .menu
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Reveal/Hide Menu
|
||||
|
||||
/// Reveals the menu.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - animated: If set to true, the process will be animated. The default is true.
|
||||
/// - completion: Completion closure that will be executed after revealing the menu.
|
||||
open func revealMenu(animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
|
||||
changeMenuVisibility(reveal: true, animated: animated, completion: completion)
|
||||
}
|
||||
|
||||
/// Hides the menu.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - animated: If set to true, the process will be animated. The default is true.
|
||||
/// - completion: Completion closure that will be executed after hiding the menu.
|
||||
open func hideMenu(animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
|
||||
changeMenuVisibility(reveal: false, animated: animated, completion: completion)
|
||||
}
|
||||
|
||||
private func changeMenuVisibility(reveal: Bool,
|
||||
animated: Bool = true,
|
||||
shouldCallDelegate: Bool = true,
|
||||
shouldChangeStatusBar: Bool = true,
|
||||
completion: ((Bool) -> Void)? = nil) {
|
||||
menuViewController.beginAppearanceTransition(reveal, animated: animated)
|
||||
|
||||
if shouldCallDelegate {
|
||||
reveal ? delegate?.sideMenuControllerWillRevealMenu(self) : delegate?.sideMenuControllerWillHideMenu(self)
|
||||
}
|
||||
|
||||
if reveal {
|
||||
addContentOverlayViewIfNeeded()
|
||||
}
|
||||
|
||||
// UIApplication.shared.beginIgnoringInteractionEvents()
|
||||
self.view.isUserInteractionEnabled = true
|
||||
|
||||
let animationClosure = {
|
||||
self.menuContainerView.frame = self.sideMenuFrame(visibility: reveal)
|
||||
self.contentContainerView.frame = self.contentFrame(visibility: reveal)
|
||||
if self.shouldShowShadowOnContent {
|
||||
self.contentContainerOverlay?.alpha = reveal ? self.preferences.animation.shadowAlpha : 0
|
||||
}
|
||||
}
|
||||
|
||||
let animationCompletionClosure: (Bool) -> Void = { finish in
|
||||
self.menuViewController.endAppearanceTransition()
|
||||
|
||||
if shouldCallDelegate {
|
||||
if reveal {
|
||||
self.delegate?.sideMenuControllerDidRevealMenu(self)
|
||||
} else {
|
||||
self.delegate?.sideMenuControllerDidHideMenu(self)
|
||||
}
|
||||
}
|
||||
|
||||
if !reveal {
|
||||
self.contentContainerOverlay?.removeFromSuperview()
|
||||
self.contentContainerOverlay = nil
|
||||
}
|
||||
|
||||
completion?(true)
|
||||
|
||||
// UIApplication.shared.endIgnoringInteractionEvents()
|
||||
// self.view.isUserInteractionEnabled = false
|
||||
self.isMenuRevealed = reveal
|
||||
}
|
||||
|
||||
if animated {
|
||||
animateMenu(with: reveal,
|
||||
shouldChangeStatusBar: shouldChangeStatusBar,
|
||||
animations: animationClosure,
|
||||
completion: animationCompletionClosure)
|
||||
} else {
|
||||
// setStatusBar(hidden: reveal)
|
||||
animationClosure()
|
||||
animationCompletionClosure(true)
|
||||
completion?(true)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private func animateMenu(with reveal: Bool,
|
||||
shouldChangeStatusBar: Bool = true,
|
||||
animations: @escaping () -> Void,
|
||||
completion: ((Bool) -> Void)? = nil) {
|
||||
// let shouldAnimateStatusBarChange = preferences.basic.statusBarBehavior != .hideOnMenu
|
||||
// if shouldChangeStatusBar && !shouldAnimateStatusBarChange && reveal {
|
||||
// setStatusBar(hidden: reveal)
|
||||
// }
|
||||
let duration = reveal ? preferences.animation.revealDuration : preferences.animation.hideDuration
|
||||
UIView.animate(withDuration: duration,
|
||||
delay: 0,
|
||||
usingSpringWithDamping: preferences.animation.dampingRatio,
|
||||
initialSpringVelocity: preferences.animation.initialSpringVelocity,
|
||||
options: preferences.animation.options,
|
||||
animations: {
|
||||
// if shouldChangeStatusBar && shouldAnimateStatusBarChange {
|
||||
// self.setStatusBar(hidden: reveal)
|
||||
// }
|
||||
|
||||
animations()
|
||||
}, completion: { (finished) in
|
||||
// if shouldChangeStatusBar && !shouldAnimateStatusBarChange && !reveal {
|
||||
// self.setStatusBar(hidden: reveal)
|
||||
// }
|
||||
|
||||
completion?(finished)
|
||||
})
|
||||
}
|
||||
|
||||
// MARK: Gesture Recognizer
|
||||
|
||||
private func configureGesturesRecognizer() {
|
||||
// The gesture will be added anyway, its delegate will tell whether it should be recognized
|
||||
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(SideMenuController.handlePanGesture(_:)))
|
||||
panGesture.delegate = self
|
||||
panGestureRecognizer = panGesture
|
||||
view.addGestureRecognizer(panGesture)
|
||||
}
|
||||
|
||||
private func addContentOverlayViewIfNeeded() {
|
||||
guard contentContainerOverlay == nil else {
|
||||
return
|
||||
}
|
||||
|
||||
var overlay:UIView
|
||||
if SideMenuController.preferences.animation.shouldAddBlurWhenRevealing {
|
||||
let blurEffect = UIBlurEffect(style: .light)
|
||||
overlay = UIVisualEffectView(effect: blurEffect)
|
||||
} else {
|
||||
overlay = UIView(frame: contentContainerView.bounds)
|
||||
}
|
||||
overlay.autoresizingMask = [.flexibleHeight, .flexibleWidth]
|
||||
|
||||
if !shouldShowShadowOnContent {
|
||||
overlay.backgroundColor = .clear
|
||||
} else {
|
||||
overlay.backgroundColor = SideMenuController.preferences.animation.shadowColor
|
||||
overlay.alpha = 0
|
||||
}
|
||||
|
||||
// UIKit can coordinate overlay's tap gesture and controller view's pan gesture correctly
|
||||
let tapToHideGesture = UITapGestureRecognizer()
|
||||
tapToHideGesture.addTarget(self, action: #selector(SideMenuController.handleTapGesture(_:)))
|
||||
overlay.addGestureRecognizer(tapToHideGesture)
|
||||
|
||||
contentContainerView.insertSubview(overlay, aboveSubview: contentViewController.view)
|
||||
contentContainerOverlay = overlay
|
||||
contentContainerOverlay?.accessibilityIdentifier = "ContentShadowOverlay"
|
||||
}
|
||||
|
||||
@objc private func handleTapGesture(_ tap: UITapGestureRecognizer) {
|
||||
hideMenu()
|
||||
}
|
||||
|
||||
@objc private func handlePanGesture(_ pan: UIPanGestureRecognizer) {
|
||||
let isLeft = adjustedDirection == .left
|
||||
var translation = pan.translation(in: pan.view).x
|
||||
let viewToAnimate: UIView
|
||||
let viewToAnimate2: UIView?
|
||||
var leftBorder: CGFloat
|
||||
var rightBorder: CGFloat
|
||||
let containerWidth: CGFloat
|
||||
switch preferences.basic.position {
|
||||
case .above:
|
||||
viewToAnimate = menuContainerView
|
||||
viewToAnimate2 = nil
|
||||
containerWidth = viewToAnimate.frame.width
|
||||
leftBorder = -containerWidth
|
||||
rightBorder = menuWidth - containerWidth
|
||||
case .under:
|
||||
viewToAnimate = contentContainerView
|
||||
viewToAnimate2 = nil
|
||||
containerWidth = viewToAnimate.frame.width
|
||||
leftBorder = 0
|
||||
rightBorder = menuWidth
|
||||
case .sideBySide:
|
||||
viewToAnimate = contentContainerView
|
||||
viewToAnimate2 = menuContainerView
|
||||
containerWidth = viewToAnimate.frame.width
|
||||
leftBorder = 0
|
||||
rightBorder = menuWidth
|
||||
}
|
||||
|
||||
if !isLeft {
|
||||
swap(&leftBorder, &rightBorder)
|
||||
leftBorder *= -1
|
||||
rightBorder *= -1
|
||||
}
|
||||
|
||||
switch pan.state {
|
||||
case .began:
|
||||
panningBeganPointX = viewToAnimate.frame.origin.x
|
||||
isValidatePanningBegan = false
|
||||
case .changed:
|
||||
let resultX = panningBeganPointX + translation
|
||||
let notReachLeftBorder = (!isLeft && preferences.basic.enableRubberEffectWhenPanning) || resultX >= leftBorder
|
||||
let notReachRightBorder = (isLeft && preferences.basic.enableRubberEffectWhenPanning) || resultX <= rightBorder
|
||||
guard notReachLeftBorder && notReachRightBorder else {
|
||||
return
|
||||
}
|
||||
|
||||
if !isValidatePanningBegan {
|
||||
// Do some setup works in the initial step of validate panning. This can't be done in the `.began` period
|
||||
// because we can't know whether its a validate panning
|
||||
addContentOverlayViewIfNeeded()
|
||||
// setStatusBar(hidden: true, animate: true)
|
||||
|
||||
isValidatePanningBegan = true
|
||||
}
|
||||
|
||||
let factor: CGFloat = isLeft ? 1 : -1
|
||||
let notReachDesiredBorder = isLeft ? resultX <= rightBorder : resultX >= leftBorder
|
||||
if notReachDesiredBorder {
|
||||
viewToAnimate.frame.origin.x = resultX
|
||||
} else {
|
||||
if !isMenuRevealed {
|
||||
translation -= menuWidth * factor
|
||||
}
|
||||
viewToAnimate.frame.origin.x = (isLeft ? rightBorder : leftBorder) + factor * menuWidth
|
||||
* log10(translation * factor / menuWidth + 1) * 0.5
|
||||
}
|
||||
|
||||
if let viewToAnimate2 = viewToAnimate2 {
|
||||
viewToAnimate2.frame.origin.x = viewToAnimate.frame.origin.x - containerWidth * factor
|
||||
}
|
||||
|
||||
if shouldShowShadowOnContent {
|
||||
let movingDistance: CGFloat
|
||||
if isLeft {
|
||||
movingDistance = menuContainerView.frame.maxX
|
||||
} else {
|
||||
movingDistance = menuWidth - menuContainerView.frame.minX
|
||||
}
|
||||
let shadowPercent = min(movingDistance / menuWidth, 1)
|
||||
contentContainerOverlay?.alpha = self.preferences.animation.shadowAlpha * shadowPercent
|
||||
}
|
||||
case .ended, .cancelled, .failed:
|
||||
let offset: CGFloat
|
||||
switch preferences.basic.position {
|
||||
case .above:
|
||||
offset = isLeft ? viewToAnimate.frame.maxX : containerWidth - viewToAnimate.frame.minX
|
||||
case .under, .sideBySide:
|
||||
offset = isLeft ? viewToAnimate.frame.minX : containerWidth - viewToAnimate.frame.maxX
|
||||
}
|
||||
let offsetPercent = offset / menuWidth
|
||||
let decisionPoint: CGFloat = isMenuRevealed ? 0.85 : 0.15
|
||||
if offsetPercent > decisionPoint {
|
||||
// We need to call the delegates, change the status bar only when the menu was previous hidden
|
||||
changeMenuVisibility(reveal: true, shouldCallDelegate: !isMenuRevealed, shouldChangeStatusBar: !isMenuRevealed)
|
||||
} else {
|
||||
changeMenuVisibility(reveal: false, shouldCallDelegate: isMenuRevealed, shouldChangeStatusBar: true)
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Notification
|
||||
|
||||
private func setUpNotifications() {
|
||||
NotificationCenter.default.addObserver(self,
|
||||
selector: #selector(SideMenuController.appDidEnteredBackground),
|
||||
name: UIApplication.didEnterBackgroundNotification,
|
||||
object: nil)
|
||||
}
|
||||
|
||||
private func unregisterNotifications() {
|
||||
// swiftlint:disable:next notification_center_detachment
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
|
||||
@objc private func appDidEnteredBackground() {
|
||||
if preferences.basic.hideMenuWhenEnteringBackground {
|
||||
hideMenu(animated: false)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Status Bar
|
||||
|
||||
// private func setStatusBar(hidden: Bool, animate: Bool = false) {
|
||||
// // UIKit provides `setNeedsStatusBarAppearanceUpdate` and couple of methods to animate the status bar changes.
|
||||
// // The problem with this approach is it will hide the status bar and it's underlying space completely, as a result,
|
||||
// // the navigation bar will go up as we don't expect.
|
||||
// // So we need to manipulate the windows of status bar manually.
|
||||
//
|
||||
// let behavior = self.preferences.basic.statusBarBehavior
|
||||
// guard let sbw = UIWindow.sb, sbw.isStatusBarHidden(with: behavior) != hidden else {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// if animate && behavior != .hideOnMenu {
|
||||
// UIView.animate(withDuration: 0.4, animations: {
|
||||
// sbw.setStatusBarHidden(hidden, with: behavior)
|
||||
// })
|
||||
// } else {
|
||||
// sbw.setStatusBarHidden(hidden, with: behavior)
|
||||
// }
|
||||
//
|
||||
// if behavior == .hideOnMenu {
|
||||
// if !hidden {
|
||||
// statusBarScreenShotView?.removeFromSuperview()
|
||||
// statusBarScreenShotView = nil
|
||||
// } else if statusBarScreenShotView == nil, let newStatusBarScreenShot = statusBarScreenShot() {
|
||||
// statusBarScreenShotView = newStatusBarScreenShot
|
||||
// contentContainerView.insertSubview(newStatusBarScreenShot, aboveSubview: contentViewController.view)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// private func statusBarScreenShot() -> UIView? {
|
||||
// let statusBarFrame = UIApplication.shared.statusBarFrame
|
||||
// let screenshot = UIScreen.main.snapshotView(afterScreenUpdates: false)
|
||||
// screenshot.frame = statusBarFrame
|
||||
// screenshot.contentMode = .top
|
||||
// screenshot.clipsToBounds = true
|
||||
// return screenshot
|
||||
// }
|
||||
|
||||
open override var childForStatusBarStyle: UIViewController? {
|
||||
// Forward to the content view controller
|
||||
return contentViewController
|
||||
}
|
||||
|
||||
open override var childForStatusBarHidden: UIViewController? {
|
||||
return contentViewController
|
||||
}
|
||||
|
||||
// MARK: Caching
|
||||
|
||||
/// Caches the closure that generate the view controller with identifier.
|
||||
///
|
||||
/// It's useful when you want to configure the caching relation without instantiating the view controller immediately.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - viewControllerGenerator: The closure that generate the view controller. It will only executed when needed.
|
||||
/// - identifier: Identifier used to change content view controller
|
||||
open func cache(viewControllerGenerator: @escaping () -> UIViewController?, with identifier: String) {
|
||||
lazyCachedViewControllerGenerators[identifier] = viewControllerGenerator
|
||||
}
|
||||
|
||||
/// Caches the view controller with identifier.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - viewController: the view controller to cache
|
||||
/// - identifier: the identifier
|
||||
open func cache(viewController: UIViewController, with identifier: String) {
|
||||
lazyCachedViewControllers[identifier] = viewController
|
||||
}
|
||||
|
||||
/// Changes the content view controller to the cached one with given `identifier`.
|
||||
/// - Parameters:
|
||||
/// - identifier: the identifier that associates with a cache view controller or generator.
|
||||
/// - animated: whether the transition should be animated, default is `false`.
|
||||
/// - completion: the completion closure will be called when the transition complete. Notice that if the caller is the current content view controller, once the transition completed, the caller will be removed from the parent view controller, and it will have no access to the side menu controller via `sideMenuController`
|
||||
open func setContentViewController(with identifier: String,
|
||||
animated: Bool = false,
|
||||
completion: (() -> Void)? = nil) {
|
||||
if let viewController = lazyCachedViewControllers[identifier] {
|
||||
setContentViewController(to: viewController, animated: animated, completion: completion)
|
||||
} else if let viewController = lazyCachedViewControllerGenerators[identifier]?() {
|
||||
lazyCachedViewControllerGenerators[identifier] = nil
|
||||
lazyCachedViewControllers[identifier] = viewController
|
||||
setContentViewController(to: viewController, animated: animated, completion: completion)
|
||||
} else {
|
||||
fatalError("[SideMenu] View controller associated with \(identifier) not found!")
|
||||
}
|
||||
}
|
||||
|
||||
/// Change the content view controller to `viewController`
|
||||
/// - Parameters:
|
||||
/// - viewController: the view controller which will become the content view controller
|
||||
/// - animated: whether the transition should be animated, default is `false`.
|
||||
/// - completion: the completion closure will be called when the transition complete. Notice that if the caller is the current content view controller, once the transition completed, the caller will be removed from the parent view
|
||||
open func setContentViewController(to viewController: UIViewController,
|
||||
animated: Bool = false,
|
||||
completion: (() -> Void)? = nil) {
|
||||
guard contentViewController !== viewController && isViewLoaded else {
|
||||
completion?()
|
||||
return
|
||||
}
|
||||
|
||||
if animated {
|
||||
delegate?.sideMenuController(self, willShow: viewController, animated: animated)
|
||||
|
||||
addChild(viewController)
|
||||
|
||||
viewController.view.frame = view.bounds
|
||||
viewController.view.translatesAutoresizingMaskIntoConstraints = true
|
||||
viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
||||
|
||||
let animatorFromDelegate = delegate?.sideMenuController(self,
|
||||
animationControllerFrom: contentViewController,
|
||||
to: viewController)
|
||||
|
||||
#if DEBUG
|
||||
if animatorFromDelegate == nil {
|
||||
// swiftlint:disable:next line_length
|
||||
print("[SideMenu] `setContentViewController` is called with animated while the delegate method return nil, fall back to the fade animation.")
|
||||
}
|
||||
#endif
|
||||
|
||||
let animator = animatorFromDelegate ?? BasicTransitionAnimator()
|
||||
|
||||
let transitionContext = SideMenuController.TransitionContext(with: contentViewController,
|
||||
toViewController: viewController)
|
||||
transitionContext.isAnimated = true
|
||||
transitionContext.isInteractive = false
|
||||
transitionContext.completion = { finish in
|
||||
self.unload(self.contentViewController)
|
||||
|
||||
self.shouldCallSwitchingDelegate = false
|
||||
// It's tricky here.
|
||||
// `contentViewController` setter won't trigger due to the `viewController` already is added to the hierarchy.
|
||||
// `shouldCallSwitchingDelegate` also prevent the delegate from been calling.
|
||||
self.contentViewController = viewController
|
||||
self.shouldCallSwitchingDelegate = true
|
||||
|
||||
self.delegate?.sideMenuController(self, didShow: viewController, animated: animated)
|
||||
|
||||
viewController.didMove(toParent: self)
|
||||
|
||||
completion?()
|
||||
}
|
||||
animator.animateTransition(using: transitionContext)
|
||||
|
||||
} else {
|
||||
contentViewController = viewController
|
||||
completion?()
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the identifier of current content view controller.
|
||||
///
|
||||
/// - Returns: if not exist, returns nil.
|
||||
open func currentCacheIdentifier() -> String? {
|
||||
guard let index = lazyCachedViewControllers.values.firstIndex(of: contentViewController) else {
|
||||
return nil
|
||||
}
|
||||
return lazyCachedViewControllers.keys[index]
|
||||
}
|
||||
|
||||
/// Clears cached view controller or generators with identifier.
|
||||
///
|
||||
/// - Parameter identifier: the identifier that associates with a cache view controller or generator.
|
||||
open func clearCache(with identifier: String) {
|
||||
lazyCachedViewControllerGenerators[identifier] = nil
|
||||
lazyCachedViewControllers[identifier] = nil
|
||||
}
|
||||
|
||||
// MARK: - Helper Methods
|
||||
|
||||
private func sideMenuFrame(visibility: Bool, targetSize: CGSize? = nil) -> CGRect {
|
||||
let position = preferences.basic.position
|
||||
switch position {
|
||||
case .above, .sideBySide:
|
||||
var baseFrame = CGRect(origin: view.frame.origin, size: targetSize ?? view.frame.size)
|
||||
if visibility {
|
||||
baseFrame.origin.x = menuWidth - baseFrame.width
|
||||
} else {
|
||||
baseFrame.origin.x = -baseFrame.width
|
||||
}
|
||||
let factor: CGFloat = adjustedDirection == .left ? 1 : -1
|
||||
baseFrame.origin.x *= factor
|
||||
return CGRect(origin: baseFrame.origin, size: targetSize ?? baseFrame.size)
|
||||
case .under:
|
||||
return CGRect(origin: view.frame.origin, size: targetSize ?? view.frame.size)
|
||||
}
|
||||
}
|
||||
|
||||
private func contentFrame(visibility: Bool, targetSize: CGSize? = nil) -> CGRect {
|
||||
let position = preferences.basic.position
|
||||
switch position {
|
||||
case .above:
|
||||
return CGRect(origin: view.frame.origin, size: targetSize ?? view.frame.size)
|
||||
case .under, .sideBySide:
|
||||
var baseFrame = CGRect(origin: view.frame.origin, size: targetSize ?? view.frame.size)
|
||||
if visibility {
|
||||
let factor: CGFloat = adjustedDirection == .left ? 1 : -1
|
||||
baseFrame.origin.x = menuWidth * factor
|
||||
} else {
|
||||
baseFrame.origin.x = 0
|
||||
}
|
||||
return CGRect(origin: baseFrame.origin, size: targetSize ?? baseFrame.size)
|
||||
}
|
||||
}
|
||||
|
||||
private func keepSideMenuOpenOnRotation() {
|
||||
guard menuViewController != nil else {
|
||||
return
|
||||
}
|
||||
|
||||
if isMenuRevealed {
|
||||
hideMenu(animated: false, completion: nil)
|
||||
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
|
||||
self.revealMenu(animated: false, completion: nil)
|
||||
})
|
||||
} else {
|
||||
revealMenu(animated: false) { _ in
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
|
||||
self.hideMenu(animated: false, completion: nil)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Orientation
|
||||
|
||||
open override var shouldAutorotate: Bool {
|
||||
if preferences.basic.shouldUseContentSupportedOrientations {
|
||||
return contentViewController.shouldAutorotate
|
||||
}
|
||||
return preferences.basic.shouldAutorotate
|
||||
}
|
||||
|
||||
open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
|
||||
if preferences.basic.shouldUseContentSupportedOrientations {
|
||||
return contentViewController.supportedInterfaceOrientations
|
||||
}
|
||||
return preferences.basic.supportedOrientations
|
||||
}
|
||||
|
||||
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
|
||||
if preferences.basic.keepsMenuOpenAfterRotation {
|
||||
keepSideMenuOpenOnRotation()
|
||||
} else {
|
||||
hideMenu(animated: false, completion: { _ in
|
||||
// Temporally hide the menu container view for smooth animation
|
||||
self.menuContainerView.isHidden = true
|
||||
coordinator.animate(alongsideTransition: { _ in
|
||||
self.contentContainerView.frame = self.contentFrame(visibility: self.isMenuRevealed, targetSize: size)
|
||||
}, completion: { (_) in
|
||||
self.menuContainerView.isHidden = false
|
||||
self.menuContainerView.frame = self.sideMenuFrame(visibility: self.isMenuRevealed, targetSize: size)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
super.viewWillTransition(to: size, with: coordinator)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: UIGestureRecognizerDelegate
|
||||
|
||||
extension SideMenuController: UIGestureRecognizerDelegate {
|
||||
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
|
||||
guard preferences.basic.enablePanGesture else {
|
||||
return false
|
||||
}
|
||||
|
||||
if let shouldReveal = self.delegate?.sideMenuControllerShouldRevealMenu(self) {
|
||||
guard shouldReveal else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
if isViewControllerInsideNavigationStack(for: touch.view) {
|
||||
return false
|
||||
}
|
||||
|
||||
if touch.view is UISlider {
|
||||
return false
|
||||
}
|
||||
|
||||
// If the view is scrollable in horizon direction, don't receive the touch
|
||||
if let scrollView = touch.view as? UIScrollView, scrollView.frame.width > scrollView.contentSize.width {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
|
||||
if let velocity = panGestureRecognizer?.velocity(in: view) {
|
||||
return isValidateHorizontalMovement(for: velocity)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private func isViewControllerInsideNavigationStack(for view: UIView?) -> Bool {
|
||||
guard let view = view,
|
||||
let viewController = view.parentViewController else {
|
||||
return false
|
||||
}
|
||||
|
||||
if let navigationController = viewController as? UINavigationController {
|
||||
return navigationController.viewControllers.count > 1
|
||||
} else if let navigationController = viewController.navigationController {
|
||||
if let index = navigationController.viewControllers.firstIndex(of: viewController) {
|
||||
return index > 0
|
||||
}
|
||||
} else {
|
||||
// Check if the ViewController is embedded
|
||||
var parent = viewController.parent
|
||||
while parent != nil {
|
||||
guard let navigationController = parent as? UINavigationController else {
|
||||
parent = parent?.parent
|
||||
continue
|
||||
}
|
||||
return navigationController.viewControllers.count > 0
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
private func isValidateHorizontalMovement(for velocity: CGPoint) -> Bool {
|
||||
if isMenuRevealed {
|
||||
return true
|
||||
}
|
||||
|
||||
let direction = preferences.basic.direction
|
||||
var factor: CGFloat = direction == .left ? 1 : -1
|
||||
factor *= shouldReverseDirection ? -1 : 1
|
||||
guard velocity.x * factor > 0 else {
|
||||
return false
|
||||
}
|
||||
return abs(velocity.y / velocity.x) < preferences.basic.panGestureSensitivity
|
||||
}
|
||||
}
|
||||
57
WOKA/TabBar & SideMenu/SideMenu/StatusBar.swift
Normal file
57
WOKA/TabBar & SideMenu/SideMenu/StatusBar.swift
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// StatusBar.swift
|
||||
// SideMenu
|
||||
//
|
||||
// Created by kukushi on 22/02/2018.
|
||||
// Copyright © 2018 kukushi. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UIWindow {
|
||||
|
||||
// swiftlint:disable identifier_name
|
||||
/// Returns current application's `statusBarWindows`
|
||||
static var sb: UIWindow? {
|
||||
// We use a non-public key here to obtain the `statusBarWindow` window.
|
||||
// We have been using it in real world app and it won't be rejected by the review team for using this key.
|
||||
let s = "status", b = "Bar", w = "Window"
|
||||
if #available(iOS 13, *) {
|
||||
return nil
|
||||
} else {
|
||||
return UIApplication.shared.value(forKey: s+b+w) as? UIWindow
|
||||
}
|
||||
}
|
||||
|
||||
/// Changes the windows' visibility with custom behavior
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - hidden: the windows hidden status
|
||||
/// - behavior: status bar behavior
|
||||
// internal func setStatusBarHidden(_ hidden: Bool, with behavior: SideMenuController.Preferences.StatusBarBehavior) {
|
||||
// guard behavior != .none else {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// switch behavior {
|
||||
// case .fade, .hideOnMenu:
|
||||
// alpha = hidden ? 0 : 1
|
||||
// case .slide:
|
||||
// let statusBarHeight = UIApplication.shared.statusBarFrame.height
|
||||
// transform = hidden ? CGAffineTransform(translationX: 0, y: -statusBarHeight) : .identity
|
||||
// default:
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// internal func isStatusBarHidden(with behavior: SideMenuController.Preferences.StatusBarBehavior) -> Bool {
|
||||
// switch behavior {
|
||||
// case .none:
|
||||
// return false
|
||||
// case .fade, .hideOnMenu:
|
||||
// return alpha == 0
|
||||
// case .slide:
|
||||
// return transform != .identity
|
||||
// }
|
||||
// }
|
||||
}
|
||||
80
WOKA/TabBar & SideMenu/SideMenu/TransitionContext.swift
Normal file
80
WOKA/TabBar & SideMenu/SideMenu/TransitionContext.swift
Normal file
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// SideMenuTransitionContext.swift
|
||||
// SideMenu
|
||||
//
|
||||
// Created by kukushi on 2018/8/8.
|
||||
// Copyright © 2018 kukushi. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
/// A internal transitioning context used for triggering the transition animation
|
||||
extension SideMenuController {
|
||||
class TransitionContext: NSObject, UIViewControllerContextTransitioning {
|
||||
var isAnimated = true
|
||||
var targetTransform: CGAffineTransform = .identity
|
||||
|
||||
let containerView: UIView
|
||||
let presentationStyle: UIModalPresentationStyle
|
||||
|
||||
private var viewControllers = [UITransitionContextViewControllerKey: UIViewController]()
|
||||
|
||||
var isInteractive = false
|
||||
|
||||
var transitionWasCancelled: Bool {
|
||||
// Our non-interactive transition can't be cancelled
|
||||
return false
|
||||
}
|
||||
|
||||
var completion: ((Bool) -> Void)?
|
||||
|
||||
init(with fromViewController: UIViewController, toViewController: UIViewController) {
|
||||
guard let superView = fromViewController.view.superview else {
|
||||
fatalError("fromViewController's view should have a parent view")
|
||||
}
|
||||
presentationStyle = .custom
|
||||
containerView = superView
|
||||
viewControllers = [
|
||||
.from: fromViewController,
|
||||
.to: toViewController
|
||||
]
|
||||
|
||||
super.init()
|
||||
}
|
||||
|
||||
func completeTransition(_ didComplete: Bool) {
|
||||
completion?(didComplete)
|
||||
}
|
||||
|
||||
func viewController(forKey key: UITransitionContextViewControllerKey) -> UIViewController? {
|
||||
return viewControllers[key]
|
||||
}
|
||||
|
||||
func view(forKey key: UITransitionContextViewKey) -> UIView? {
|
||||
switch key {
|
||||
case .from:
|
||||
return viewControllers[.from]?.view
|
||||
case .to:
|
||||
return viewControllers[.to]?.view
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// swiftlint:disable identifier_name
|
||||
func initialFrame(for vc: UIViewController) -> CGRect {
|
||||
return containerView.frame
|
||||
}
|
||||
|
||||
func finalFrame(for vc: UIViewController) -> CGRect {
|
||||
return containerView.frame
|
||||
}
|
||||
|
||||
// MARK: Interactive, not supported yet
|
||||
|
||||
func updateInteractiveTransition(_ percentComplete: CGFloat) {}
|
||||
func finishInteractiveTransition() {}
|
||||
func cancelInteractiveTransition() {}
|
||||
func pauseInteractiveTransition() {}
|
||||
}
|
||||
}
|
||||
31
WOKA/TabBar & SideMenu/SideMenu/UIView+Container.swift
Normal file
31
WOKA/TabBar & SideMenu/SideMenu/UIView+Container.swift
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// UIView+Container.swift
|
||||
// SideMenu
|
||||
//
|
||||
// Created by kukushi on 2018/9/12.
|
||||
// Copyright © 2018 kukushi. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UIView {
|
||||
var parentViewController: UIViewController? {
|
||||
var parentResponder: UIResponder? = self
|
||||
while parentResponder != nil {
|
||||
// swiftlint:disable:next force_unwrapping
|
||||
parentResponder = parentResponder!.next
|
||||
if let viewController = parentResponder as? UIViewController {
|
||||
return viewController
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var parentNavigationController: UINavigationController? {
|
||||
let currentViewController = parentViewController
|
||||
if let navigationController = currentViewController as? UINavigationController {
|
||||
return navigationController
|
||||
}
|
||||
return currentViewController?.navigationController
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// UIViewController+Container.swift
|
||||
// SideMenu
|
||||
//
|
||||
// Created by kukushi on 2018/8/8.
|
||||
// Copyright © 2018 kukushi. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UIViewController {
|
||||
|
||||
func load(_ viewController: UIViewController?, on view: UIView) {
|
||||
guard let viewController = viewController else {
|
||||
return
|
||||
}
|
||||
|
||||
// `willMoveToParentViewController:` is called automatically when adding
|
||||
|
||||
addChild(viewController)
|
||||
|
||||
viewController.view.frame = view.bounds
|
||||
viewController.view.translatesAutoresizingMaskIntoConstraints = true
|
||||
viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
||||
|
||||
view.addSubview(viewController.view)
|
||||
|
||||
viewController.didMove(toParent: self)
|
||||
}
|
||||
|
||||
func unload(_ viewController: UIViewController?) {
|
||||
guard let viewController = viewController else {
|
||||
return
|
||||
}
|
||||
|
||||
viewController.willMove(toParent: nil)
|
||||
viewController.view.removeFromSuperview()
|
||||
viewController.removeFromParent()
|
||||
// `didMoveToParentViewController:` is called automatically when removing
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// UIViewController+Extension.swift
|
||||
// SideMenu
|
||||
//
|
||||
// Created by kukushi on 10/02/2018.
|
||||
// Copyright © 2018 kukushi. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
// Provides access to the side menu controller
|
||||
public extension UIViewController {
|
||||
|
||||
/// Access the nearest ancestor view controller hierarchy that is a side menu controller.
|
||||
var sideMenuController: SideMenuController? {
|
||||
return findSideMenuController(from: self)
|
||||
}
|
||||
|
||||
fileprivate func findSideMenuController(from viewController: UIViewController) -> SideMenuController? {
|
||||
var sourceViewController: UIViewController? = viewController
|
||||
repeat {
|
||||
sourceViewController = sourceViewController?.parent
|
||||
if let sideMenuController = sourceViewController as? SideMenuController {
|
||||
return sideMenuController
|
||||
}
|
||||
} while (sourceViewController != nil)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
118
WOKA/TabBar & SideMenu/SideMenuVC.swift
Normal file
118
WOKA/TabBar & SideMenu/SideMenuVC.swift
Normal file
@@ -0,0 +1,118 @@
|
||||
//
|
||||
// SideMenuVC.swift
|
||||
// WOKA
|
||||
//
|
||||
// Created by MacBook Pro on 21/05/24.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
class SideMenuVC: UIViewController {
|
||||
|
||||
@IBOutlet weak var logoutBtn: UIButton!
|
||||
@IBOutlet weak var selectionMenuTrailingConstraint: NSLayoutConstraint!
|
||||
@IBOutlet weak var languageControl: CustomizableSegmentControl!
|
||||
|
||||
@IBOutlet weak var theme1: UIImageView!
|
||||
@IBOutlet weak var theme2: UIImageView!
|
||||
|
||||
var vm = SideMenuVM()
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
vm.vc = self
|
||||
vm.initView()
|
||||
|
||||
}
|
||||
|
||||
@IBAction func closeBtnTapped(_ sender: UIButton) {
|
||||
self.sideMenuController?.hideMenu()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class CustomizableSegmentControl: UISegmentedControl {
|
||||
|
||||
private(set) lazy var radius:CGFloat = bounds.height / 2
|
||||
|
||||
private let segmentInset: CGFloat = 5 //your inset amount
|
||||
private let segmentImage: UIImage? = UIImage(color: UIColor.white) //your color
|
||||
|
||||
override init(items: [Any]?) {
|
||||
super.init(items: items)
|
||||
selectedSegmentIndex = 0
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
// fatalError("init(coder:) has not been implemented")
|
||||
super.init(coder: coder)
|
||||
}
|
||||
|
||||
|
||||
override func layoutSubviews(){
|
||||
super.layoutSubviews()
|
||||
|
||||
self.backgroundColor = #colorLiteral(red: 0.01960784314, green: 0, blue: 0.2196078431, alpha: 1)
|
||||
|
||||
// selected option color
|
||||
self.setTitleTextAttributes([.foregroundColor: UIColor.black, .font : UIFont.systemFont(ofSize: 16, weight: .heavy)], for: .selected)
|
||||
|
||||
// color of other options
|
||||
self.setTitleTextAttributes([.foregroundColor: UIColor.white, .font : UIFont.systemFont(ofSize: 16, weight: .heavy)], for: .normal)
|
||||
//background
|
||||
layer.cornerRadius = radius
|
||||
//foreground
|
||||
let foregroundIndex = numberOfSegments
|
||||
if subviews.indices.contains(foregroundIndex), let foregroundImageView = subviews[foregroundIndex] as? UIImageView{
|
||||
foregroundImageView.bounds = foregroundImageView.bounds.insetBy(dx: segmentInset, dy: segmentInset)
|
||||
foregroundImageView.image = segmentImage //substitute with our own colored image
|
||||
foregroundImageView.layer.removeAnimation(forKey: "SelectionBounds") //this removes the weird scaling animation!
|
||||
foregroundImageView.layer.masksToBounds = true
|
||||
foregroundImageView.layer.cornerRadius = foregroundImageView.bounds.height/2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension UIImage{
|
||||
|
||||
//creates a UIImage given a UIColor
|
||||
public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
|
||||
let rect = CGRect(origin: .zero, size: size)
|
||||
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
|
||||
color.setFill()
|
||||
UIRectFill(rect)
|
||||
let image = UIGraphicsGetImageFromCurrentImageContext()
|
||||
UIGraphicsEndImageContext()
|
||||
|
||||
guard let cgImage = image?.cgImage else { return nil }
|
||||
self.init(cgImage: cgImage)
|
||||
}
|
||||
}
|
||||
|
||||
//extension UIView {
|
||||
//
|
||||
// /// Apply gradient colors to the view.
|
||||
// ///
|
||||
// /// - Parameters:
|
||||
// /// - colors: An array of UIColor objects defining the color of each gradient stop.
|
||||
// /// - startPoint: The start point of the gradient, defined in the unit coordinate space. The start point corresponds to the top-left corner of the layer's bounds rectangle.
|
||||
// /// - endPoint: The end point of the gradient, defined in the unit coordinate space. The end point corresponds to the bottom-right corner of the layer's bounds rectangle.
|
||||
// ///
|
||||
// func applyGradient(colors: [UIColor], startPoint: CGPoint, endPoint: CGPoint) {
|
||||
// // Create a new CAGradientLayer instance
|
||||
// let gradientLayer = CAGradientLayer()
|
||||
//
|
||||
// // Set the frame of the gradient layer to match the bounds of the view
|
||||
// gradientLayer.frame = bounds
|
||||
//
|
||||
// // Convert the array of UIColor objects to an array of CGColor objects
|
||||
// gradientLayer.colors = colors.map { $0.cgColor }
|
||||
//
|
||||
// // Set the start and end points of the gradient
|
||||
// gradientLayer.startPoint = startPoint
|
||||
// gradientLayer.endPoint = endPoint
|
||||
//
|
||||
// // Insert the gradient layer as the bottom layer of the view's layer hierarchy
|
||||
// layer.insertSublayer(gradientLayer, at: 0)
|
||||
// }
|
||||
//}
|
||||
94
WOKA/TabBar & SideMenu/TabBarVC.swift
Normal file
94
WOKA/TabBar & SideMenu/TabBarVC.swift
Normal file
@@ -0,0 +1,94 @@
|
||||
// TabBarVC
|
||||
//
|
||||
// Created by MacBook Pro on 14/05/24.
|
||||
//
|
||||
|
||||
|
||||
import UIKit
|
||||
|
||||
class TabBarVC: UITabBarController {
|
||||
|
||||
var upperLineView: UIView!
|
||||
|
||||
let spacing: CGFloat = 12
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
self.delegate = self
|
||||
customizeTabBarItemFont()
|
||||
|
||||
UITabBar.appearance().tintColor = #colorLiteral(red: 0.0349480696, green: 0.0643344149, blue: 0.4407724142, alpha: 1)
|
||||
// UITabBar.appearance().unselectedItemTintColor = #colorLiteral(red: 0.0349480696, green: 0.0643344149, blue: 0.4407724142, alpha: 1)
|
||||
UITabBar.appearance().unselectedItemTintColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
|
||||
}
|
||||
|
||||
override func viewDidLayoutSubviews() {
|
||||
super.viewDidLayoutSubviews()
|
||||
tabBar.frame.size.height = 95
|
||||
tabBar.frame.origin.y = view.frame.height - 95
|
||||
}
|
||||
|
||||
// function to set tab bar item font
|
||||
func customizeTabBarItemFont() {
|
||||
// Set the font attributes
|
||||
let attributes = [NSAttributedString.Key.font: FontCustom.shareInstance.customFont(fontName: .Exo2_Bold, size: 12)]
|
||||
|
||||
// Apply the attributes to the appearance proxy of UITabBarItem
|
||||
UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)
|
||||
}
|
||||
|
||||
// function to customize tab bar item icon size
|
||||
func customizeTabBarItemIconSize() {
|
||||
// Get a reference to the tab bar controller
|
||||
if let tabBarController = self.tabBarController {
|
||||
// Loop through each tab bar item
|
||||
for item in tabBarController.tabBar.items! {
|
||||
// Adjust the image insets to increase the icon size
|
||||
item.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -8, right: 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///Add tabbar item indicator uper line
|
||||
// func addTabbarIndicatorView(index: Int, isFirstTime: Bool = false){
|
||||
// guard let tabView = tabBar.items?[index].value(forKey: "view") as? UIView else {
|
||||
// return
|
||||
// }
|
||||
// if !isFirstTime{
|
||||
// upperLineView.removeFromSuperview()
|
||||
// }
|
||||
// upperLineView = UIView(frame: CGRect(x: tabView.frame.minX + spacing, y: tabView.frame.minY + 0.1, width: tabView.frame.size.width - spacing * 2, height: 4))
|
||||
// upperLineView.backgroundColor = UIColor.systemPink
|
||||
// tabBar.addSubview(upperLineView)
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
extension TabBarVC: UITabBarControllerDelegate {
|
||||
|
||||
// Implement the tabBarController(_:didSelect:) method to handle tab changes
|
||||
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
|
||||
// This method will be called whenever a tab is selected
|
||||
// Do something based on the selected index
|
||||
print("Selected tab index: \(tabBarController.selectedIndex)")
|
||||
|
||||
if tabBarController.selectedIndex == 1{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Implement the tabBarController(_:shouldSelect:) method to control tab selection
|
||||
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
|
||||
|
||||
if viewController == tabBarController.viewControllers?[1] {
|
||||
let sb = UIStoryboard(name: "Home", bundle: nil)
|
||||
let vc = sb.instantiateViewController(withIdentifier: "ExploreWokaVC") as! ExploreWokaVC
|
||||
vc.modalPresentationStyle = .overCurrentContext
|
||||
vc.modalTransitionStyle = .crossDissolve
|
||||
present(vc, animated: true, completion: nil)
|
||||
return false
|
||||
}
|
||||
// Return false to prevent tab selection
|
||||
return true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user