Skip to content

Commit

Permalink
Merge pull request #20 from bguidolim/custom-button-spacing
Browse files Browse the repository at this point in the history
Custom button padding
  • Loading branch information
pedrommcarrasco committed Aug 12, 2021
2 parents 4945ea1 + 68ff344 commit ad4879c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Hover.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'Hover'
spec.version = '1.5.0'
spec.version = '2.0.0'
spec.license = { :type => 'MIT', :file => 'LICENSE' }
spec.homepage = 'https://github.com/pedrommcarrasco/Hover'
spec.authors = { 'Pedro Carrasco' => 'https://twitter.com/pedrommcarrasco' }
Expand Down
6 changes: 3 additions & 3 deletions Hover/Model/HoverConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct HoverConfiguration {
/// Dictates the size of the image shown in any button (imageSize = size * imageSizeRatio)
public var imageSizeRatio: CGFloat
/// Spacing between the floating button to the edges
public var spacing: CGFloat
public var padding: UIEdgeInsets
/// Font used in items' labels
public var font: UIFont?
/// Color of the overlay
Expand All @@ -53,7 +53,7 @@ public struct HoverConfiguration {
color: HoverColor = .color(.blue),
size: CGFloat = 60.0,
imageSizeRatio: CGFloat = 0.4,
spacing: CGFloat = 12.0,
padding: UIEdgeInsets = .init(top: 12, left: 12, bottom: 12, right: 12),
font: UIFont? = nil,
dimColor: UIColor = UIColor.black.withAlphaComponent(0.75),
initialPosition: HoverPosition = .bottomRight,
Expand All @@ -64,7 +64,7 @@ public struct HoverConfiguration {
self.imageExpandAnimation = imageExpandAnimation
self.size = size
self.imageSizeRatio = imageSizeRatio
self.spacing = spacing
self.padding = padding
self.font = font
self.dimColor = dimColor
self.initialPosition = initialPosition
Expand Down
18 changes: 9 additions & 9 deletions Hover/Model/HoverPosition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,28 @@ extension HoverPosition {
// MARK: - Configuration
extension HoverPosition {

func configurePosition(of guide: UILayoutGuide, inside view: UIView, with spacing: CGFloat) {
func configurePosition(of guide: UILayoutGuide, inside view: UIView, with insets: UIEdgeInsets) {
let positionConstraints: [NSLayoutConstraint]
switch self {
case .topLeft:
positionConstraints = [
guide.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: spacing),
guide.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: spacing)
guide.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: insets.top),
guide.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: insets.left)
]
case .topRight:
positionConstraints = [
guide.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: spacing),
guide.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -spacing)
guide.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: insets.top),
guide.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -insets.right)
]
case .bottomLeft:
positionConstraints = [
guide.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -spacing),
guide.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: spacing)
guide.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -insets.bottom),
guide.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: insets.left)
]
case .bottomRight:
positionConstraints = [
guide.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -spacing),
guide.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -spacing)
guide.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -insets.bottom),
guide.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -insets.right)
]
}
NSLayoutConstraint.activate(positionConstraints)
Expand Down
28 changes: 18 additions & 10 deletions Hover/UI/HoverView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public extension HoverView {
let hitView = super.hitTest(point, with: event)
if hitView == self {
onTouchInDim()
return nil
if state == .none { return nil }
}
return hitView
}
Expand All @@ -156,7 +156,7 @@ private extension HoverView {

func defineConstraints() {
anchors.forEach {
$0.position.configurePosition(of: $0.guide, inside: self, with: self.configuration.spacing)
$0.position.configurePosition(of: $0.guide, inside: self, with: self.configuration.padding)
NSLayoutConstraint.activate(
[
$0.guide.widthAnchor.constraint(equalToConstant: self.configuration.size),
Expand Down Expand Up @@ -309,20 +309,28 @@ private extension HoverView {
switch anchor.position {
case .topLeft:
itemsStackView.add(arrangedViews: itemViews.reversed(), hidden: true)
stackViewXConstraint = itemsStackView.leadingAnchor.constraint(equalTo: anchor.guide.leadingAnchor, constant: self.configuration.itemConfiguration.margin)
stackViewYConstraint = itemsStackView.topAnchor.constraint(equalTo: currentAnchor.guide.bottomAnchor, constant: self.configuration.spacing)
stackViewXConstraint = itemsStackView.leadingAnchor.constraint(equalTo: anchor.guide.leadingAnchor,
constant: self.configuration.itemConfiguration.margin)
stackViewYConstraint = itemsStackView.topAnchor.constraint(equalTo: currentAnchor.guide.bottomAnchor,
constant: self.configuration.padding.bottom)
case .topRight:
itemsStackView.add(arrangedViews: itemViews.reversed(), hidden: true)
stackViewXConstraint = itemsStackView.trailingAnchor.constraint(equalTo: anchor.guide.trailingAnchor, constant: -self.configuration.itemConfiguration.margin)
stackViewYConstraint = itemsStackView.topAnchor.constraint(equalTo: currentAnchor.guide.bottomAnchor, constant: self.configuration.spacing)
stackViewXConstraint = itemsStackView.trailingAnchor.constraint(equalTo: anchor.guide.trailingAnchor,
constant: -self.configuration.itemConfiguration.margin)
stackViewYConstraint = itemsStackView.topAnchor.constraint(equalTo: currentAnchor.guide.bottomAnchor,
constant: self.configuration.padding.bottom)
case .bottomLeft:
itemsStackView.add(arrangedViews: itemViews, hidden: true)
stackViewXConstraint = itemsStackView.leadingAnchor.constraint(equalTo: anchor.guide.leadingAnchor, constant: self.configuration.itemConfiguration.margin)
stackViewYConstraint = itemsStackView.bottomAnchor.constraint(equalTo: currentAnchor.guide.topAnchor, constant: -self.configuration.spacing)
stackViewXConstraint = itemsStackView.leadingAnchor.constraint(equalTo: anchor.guide.leadingAnchor,
constant: self.configuration.itemConfiguration.margin)
stackViewYConstraint = itemsStackView.bottomAnchor.constraint(equalTo: currentAnchor.guide.topAnchor,
constant: -self.configuration.padding.top)
case .bottomRight:
itemsStackView.add(arrangedViews: itemViews, hidden: true)
stackViewXConstraint = itemsStackView.trailingAnchor.constraint(equalTo: anchor.guide.trailingAnchor, constant: -self.configuration.itemConfiguration.margin)
stackViewYConstraint = itemsStackView.bottomAnchor.constraint(equalTo: currentAnchor.guide.topAnchor, constant: -self.configuration.spacing)
stackViewXConstraint = itemsStackView.trailingAnchor.constraint(equalTo: anchor.guide.trailingAnchor,
constant: -self.configuration.itemConfiguration.margin)
stackViewYConstraint = itemsStackView.bottomAnchor.constraint(equalTo: currentAnchor.guide.topAnchor,
constant: -self.configuration.padding.top)
}
NSLayoutConstraint.activate([stackViewXConstraint, stackViewYConstraint])

Expand Down

0 comments on commit ad4879c

Please sign in to comment.