Skip to content

Commit

Permalink
Fix manager.addAction will crash, and other fixes
Browse files Browse the repository at this point in the history
Speed up dismissal duration
Add dismiss handler
Add handler for each action
  • Loading branch information
CaliCastle committed Apr 17, 2018
1 parent 20a6724 commit ecb37fc
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Example/Example/View Controllers/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ class RootViewController: UITableViewController {
// Since `UIBarButtonItem` is not a subclass of `UIView`, we need to
// know the view's frame to make the relative position work
controller.setBarButtonItemForSourceView(barButtonItem)

controller.delegate = self

controller.dismissalHandler = { selected in
print("Selected: \(selected ? "Yep" : "Nope")")
}

// Present menu controller
present(controller, animated: true, completion: nil)
}
Expand Down
10 changes: 9 additions & 1 deletion PopMenu/Classes/PopMenuManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ final public class PopMenuManager: NSObject {
/// Determines whether to dismiss menu after an action is selected.
public var popMenuShouldDismissOnSelection: Bool = true

/// The dismissal handler for pop menu.
public var popMenuDismissalHandler: ((Bool) -> Void)?

/// Appearance for passing on to pop menu.
public let popMenuAppearance: PopMenuAppearance

Expand All @@ -47,6 +50,7 @@ final public class PopMenuManager: NSObject {
popMenu.delegate = popMenuDelegate
popMenu.appearance = popMenuAppearance
popMenu.shouldDismissOnSelection = popMenuShouldDismissOnSelection
popMenu.dismissalHandler = popMenuDismissalHandler

if let barButtonItem = barButtonItem {
popMenu.setBarButtonItemForSourceView(barButtonItem)
Expand All @@ -60,7 +64,11 @@ final public class PopMenuManager: NSObject {

/// Pass a new action to pop menu.
public func addAction(_ action: PopMenuAction) {
popMenu.addAction(action)
if let popMenu = popMenu {
popMenu.addAction(action)
} else {
actions.append(action)
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final public class PopMenuDismissAnimationController: NSObject, UIViewController

/// Duration of the transition.
public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.142
return 0.0982
}

/// Animate PopMenuViewController custom transition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final public class PopMenuPresentAnimationController: NSObject, UIViewController

/// Duration of the transition.
public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.142
return 0.138
}

/// Animate PopMenuViewController custom transition.
Expand Down
21 changes: 18 additions & 3 deletions PopMenu/View Controller & Views/PopMenuAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import UIKit
/// The initial color of the action.
var color: Color? { get }

/// The handler of action.
var handler: PopMenuActionHandler? { get }

/// Left padding when texts-only.
static var textLeftPadding: CGFloat { get }

Expand All @@ -49,6 +52,9 @@ import UIKit

/// Called when the action gets selected.
@objc optional func actionSelected(animated: Bool)

/// Type alias for selection handler.
typealias PopMenuActionHandler = (PopMenuAction) -> Void

}

Expand All @@ -67,6 +73,9 @@ public class PopMenuDefaultAction: NSObject, PopMenuAction {
/// Color of action.
public let color: Color?

/// Handler of action when selected.
public let handler: PopMenuActionHandler?

// MARK: - Computed Properties

/// Text color of the label.
Expand Down Expand Up @@ -142,10 +151,12 @@ public class PopMenuDefaultAction: NSObject, PopMenuAction {

// MARK: - Initializer

public init(title: String? = nil, image: UIImage? = nil, color: Color? = nil) {
/// Initializer.
public init(title: String? = nil, image: UIImage? = nil, color: Color? = nil, handler: PopMenuActionHandler? = nil) {
self.title = title
self.image = image
self.color = color
self.handler = handler

view = UIView()
}
Expand Down Expand Up @@ -196,12 +207,16 @@ public class PopMenuDefaultAction: NSObject, PopMenuAction {

/// When the action is selected.
public func actionSelected(animated: Bool) {
// Trigger handler.
handler?(self)

// Animate selection
guard animated else { return }

DispatchQueue.main.async {
UIView.animate(withDuration: 0.175, animations: {
self.view.transform = CGAffineTransform.identity.scaledBy(x: 0.92, y: 0.92)
self.view.backgroundColor = self.backgroundColor.withAlphaComponent(0.2)
self.view.transform = CGAffineTransform.identity.scaledBy(x: 0.915, y: 0.915)
self.view.backgroundColor = self.backgroundColor.withAlphaComponent(0.18)
}, completion: {
if $0 {
UIView.animate(withDuration: 0.175, animations: {
Expand Down
20 changes: 15 additions & 5 deletions PopMenu/View Controller & Views/PopMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ final public class PopMenuViewController: UIViewController {
/// Determines whether the pan gesture is enabled on the actions.
public var shouldEnablePanGesture: Bool = true

/// Handler for when the menu is dismissed.
public var dismissalHandler: ((Bool) -> Void)?

// MARK: - Constraints

private(set) var contentLeftConstraint: NSLayoutConstraint!
Expand Down Expand Up @@ -95,7 +98,7 @@ final public class PopMenuViewController: UIViewController {

/// Max content width allowed for the content to stretch to.
fileprivate let maxContentWidth: CGFloat = UIScreen.main.bounds.size.width * 0.9

// MARK: - View Life Cycle

public convenience init(sourceView: UIView? = nil, actions: [PopMenuAction], appearance: PopMenuAppearance? = nil) {
Expand Down Expand Up @@ -426,7 +429,10 @@ extension PopMenuViewController {
@objc fileprivate func backgroundViewDidTap(_ gesture: UITapGestureRecognizer) {
guard gesture.isEqual(tapGestureForDismissal), !touchedInsideContent(location: gesture.location(in: view)) else { return }

dismiss(animated: true, completion: nil)
dismiss(animated: true) {
// No selection made.
self.dismissalHandler?(false)
}
}

/// When the menu action gets tapped.
Expand Down Expand Up @@ -493,13 +499,17 @@ extension PopMenuViewController {
fileprivate func actionDidSelect(at index: Int, animated: Bool = true) {
let action = actions[index]
action.actionSelected?(animated: animated)

// Generate haptics
Haptic.impact(.medium).generate()

// Notify delegate
delegate?.popMenuDidSelectItem?(self, at: index)

// Should dismiss or not
if shouldDismissOnSelection {
dismiss(animated: true, completion: nil)
dismiss(animated: true) {
// Selection made.
self.dismissalHandler?(true)
}
}
}

Expand Down

0 comments on commit ecb37fc

Please sign in to comment.