Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make pop menu above source view #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions PopMenu/Classes/Helpers/Haptics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
import UIKit

/// Haptic Generator Helper.
@available(iOS 10.0, *)
public enum Haptic {

/// Impact style.
@available(iOS 10.0, *)
case impact(UIImpactFeedbackGenerator.FeedbackStyle)

/// Notification style.
@available(iOS 10.0, *)
case notification(UINotificationFeedbackGenerator.FeedbackType)

/// Selection style.
Expand Down
3 changes: 3 additions & 0 deletions PopMenu/Classes/Helpers/UIView+Shadows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ extension UIView {
layer.masksToBounds = false
}

public func addShadow(_ setting: PopMenuShadowColor) {
addShadow(offset: setting.offset, opacity: setting.opacity, radius: setting.radius, color: setting.color)
}
}
31 changes: 31 additions & 0 deletions PopMenu/Classes/PopMenuAppearance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ final public class PopMenuAppearance: NSObject {

/// The presentation style
public var popMenuPresentationStyle: PopMenuPresentationStyle = .cover()

/// The menu will overlay on source view or not
public var popMenuAboveSourceView = false

/// The menu shadow setting
public var popMenuShadowColor: PopMenuShadowColor = .default()
}

/// Background styles for PopMenu.
Expand Down Expand Up @@ -199,3 +204,29 @@ public enum PopMenuDirection {
case bottom
case none
}

/// Menu Shadow setting structure to control PopMenu shadow.
public struct PopMenuShadowColor {

/// Shadow offset.
public let offset: CGSize

/// Shadow opacity.
public let opacity: Float

/// Shadow radius.
public let radius: CGFloat

/// Shadow color.
public let color: Color

/// Get shadow's color instance with default setting
public static func `default`() -> PopMenuShadowColor {
return PopMenuShadowColor(offset: .init(width: 0, height: 1), opacity: 0.5, radius: 20, color: .black)
}

/// Get shadow's color instance with custom setting
public static func custom(offset: CGSize = .zero, opacity: Float = 0.65, radius: CGFloat = 20, color: Color = .black) -> PopMenuShadowColor {
return PopMenuShadowColor(offset: offset, opacity: opacity, radius: radius, color: color)
}
}
25 changes: 23 additions & 2 deletions PopMenu/View Controller & Views/PopMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ extension PopMenuViewController {
/// Setup the content view.
fileprivate func configureContentView() {
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.addShadow(offset: .init(width: 0, height: 1), opacity: 0.5, radius: 20)
containerView.addShadow(appearance.popMenuShadowColor)
containerView.layer.cornerRadius = appearance.popMenuCornerRadius
containerView.backgroundColor = .clear

Expand Down Expand Up @@ -361,6 +361,23 @@ extension PopMenuViewController {
desiredOrigin.x = minContentPos
}

if appearance.popMenuAboveSourceView {
let minContentPos: CGFloat = UIScreen.main.bounds.size.height * 0.05
let maxContentPos: CGFloat = UIScreen.main.bounds.size.height * 0.95

let offsetY = sourceFrame.size.height
desiredOrigin.y += offsetY
if (desiredOrigin.y + size.height) > maxContentPos {
desiredOrigin.y -= 2 * offsetY
}
if (desiredOrigin.y + size.height) > maxContentPos {
desiredOrigin.y = maxContentPos - size.height
}
if desiredOrigin.y < minContentPos {
desiredOrigin.y = minContentPos
}
}

// Move content in place
translateOverflowX(desiredOrigin: &desiredOrigin, contentSize: size)
translateOverflowY(desiredOrigin: &desiredOrigin, contentSize: size)
Expand Down Expand Up @@ -565,7 +582,11 @@ extension PopMenuViewController {
guard !action.highlighted else { return }

if shouldEnableHaptics {
Haptic.selection.generate()
if #available(iOS 10.0, *) {
Haptic.selection.generate()
} else {
// Fallback on earlier versions
}
}

// Highlight current action view.
Expand Down