Skip to content

Commit

Permalink
feat: support keeps open when rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
kukushi committed Feb 11, 2024
1 parent 752e1e6 commit db5812b
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 68 deletions.
97 changes: 58 additions & 39 deletions Example/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Example/MenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ extension MenuViewController: SideMenuControllerDelegate {
func sideMenuControllerDidRevealMenu(_ sideMenuController: SideMenuController) {
print("[Example] Menu did reveal.")
}

func sideMenuControllerGetMenuWidth(_ sideMenuController: SideMenuController, for size: CGSize) -> CGFloat? {
if size.width > size.height {
return 480
}
return 240
}
}

extension MenuViewController: UITableViewDelegate, UITableViewDataSource {
Expand Down
3 changes: 3 additions & 0 deletions Example/PreferencesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class PreferencesViewController: UIViewController {
@IBOutlet weak var enablePanGesture: UISwitch!
@IBOutlet weak var enableRubberBandEffect: UISwitch!
@IBOutlet weak var enableTransitionAnimationSwitch: UISwitch!
@IBOutlet weak var keepsOpenSwitch: UISwitch!
@IBOutlet weak var statusBarBehaviorSegment: UISegmentedControl!
@IBOutlet weak var menuPositionSegment: UISegmentedControl!
@IBOutlet weak var menuDirectionSegment: UISegmentedControl!
Expand Down Expand Up @@ -134,6 +135,8 @@ class PreferencesViewController: UIViewController {
SideMenuController.preferences.basic.enableRubberEffectWhenPanning = sender.isOn
case enableTransitionAnimationSwitch:
Preferences.shared.enableTransitionAnimation = sender.isOn
case keepsOpenSwitch:
SideMenuController.preferences.basic.keepsMenuOpenAfterRotation = sender.isOn
default:
break
}
Expand Down
8 changes: 7 additions & 1 deletion Sources/SideMenu/Delegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public protocol SideMenuControllerDelegate: AnyObject {
///
/// - 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
Expand All @@ -89,9 +94,10 @@ public extension SideMenuControllerDelegate {
func sideMenuController(_ sideMenuController: SideMenuController,
didShow viewController: UIViewController,
animated: Bool) {}
func sideMenuControllerShouldRevealMenu(_ sideMenuController: SideMenuController) -> Bool { return true }
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 }
}
4 changes: 2 additions & 2 deletions Sources/SideMenu/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ extension SideMenuController {
/// 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 shouldKeepMenuOpen: Bool = false
/// If the side menu should keep open on rotation. Default is `false`.
public var keepsMenuOpenAfterRotation: Bool = false
}

/// The basic configuration of side menu.
Expand Down
49 changes: 23 additions & 26 deletions Sources/SideMenu/SideMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ open class SideMenuController: UIViewController {
/// Configure this property to change the behavior of SideMenuController;
public static var preferences = Preferences()
private var preferences: Preferences {
return type(of: self).preferences
Self.preferences
}

private lazy var adjustedDirection = Preferences.MenuDirection.left

private var isInitiatedFromStoryboard: Bool {
return storyboard != nil
storyboard != nil
}

private var menuWidth: CGFloat {
delegate?.sideMenuControllerGetMenuWidth(self, for: view.frame.size) ?? preferences.basic.menuWidth
}

/// The identifier of content view controller segue.
Expand Down Expand Up @@ -379,7 +383,6 @@ open class SideMenuController: UIViewController {
}

@objc private func handlePanGesture(_ pan: UIPanGestureRecognizer) {
let menuWidth = preferences.basic.menuWidth
let isLeft = adjustedDirection == .left
var translation = pan.translation(in: pan.view).x
let viewToAnimate: UIView
Expand Down Expand Up @@ -682,7 +685,7 @@ open class SideMenuController: UIViewController {
case .above, .sideBySide:
var baseFrame = CGRect(origin: view.frame.origin, size: targetSize ?? view.frame.size)
if visibility {
baseFrame.origin.x = preferences.basic.menuWidth - baseFrame.width
baseFrame.origin.x = menuWidth - baseFrame.width
} else {
baseFrame.origin.x = -baseFrame.width
}
Expand All @@ -703,36 +706,30 @@ open class SideMenuController: UIViewController {
var baseFrame = CGRect(origin: view.frame.origin, size: targetSize ?? view.frame.size)
if visibility {
let factor: CGFloat = adjustedDirection == .left ? 1 : -1
baseFrame.origin.x = preferences.basic.menuWidth * factor
baseFrame.origin.x = menuWidth * factor
} else {
baseFrame.origin.x = 0
}
return CGRect(origin: baseFrame.origin, size: targetSize ?? baseFrame.size)
}
}

private func updateSideMenuWidth() {
let fifteenPercent = 0.15
let size = UIScreen.main.bounds.size
SideMenuController.preferences.basic.menuWidth = size.width - (size.width * CGFloat(fifteenPercent))
}

private func keepSideMenuOpenOnRotation() {
if menuViewController != nil {
if self.isMenuRevealed {
self.hideMenu(animated: false, completion: nil)
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.updateSideMenuWidth()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
self.revealMenu(animated: false, completion: nil)
})
self.hideMenu(animated: false, completion: nil)
})
} else {
self.revealMenu(animated: false) { _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
self.hideMenu(animated: false, completion: nil)
})
}
}
}
}
Expand All @@ -754,8 +751,8 @@ open class SideMenuController: UIViewController {
}

open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if preferences.basic.shouldKeepMenuOpen {
self.keepSideMenuOpenOnRotation()
if preferences.basic.keepsMenuOpenAfterRotation {
keepSideMenuOpenOnRotation()
} else {
hideMenu(animated: false, completion: { _ in
// Temporally hide the menu container view for smooth animation
Expand Down

0 comments on commit db5812b

Please sign in to comment.