Skip to content
This repository was archived by the owner on Mar 21, 2026. It is now read-only.
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
33 changes: 26 additions & 7 deletions ExpandableButton/ExpandableButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@

import UIKit

public protocol ExpandableButtonDelegate: class {
func willOpen(expandableButtonView: ExpandableButtonView)
func didOpen(expandableButtonView: ExpandableButtonView)
func willClose(expandableButtonView: ExpandableButtonView)
func didClose(expandableButtonView: ExpandableButtonView)
}

public extension ExpandableButtonDelegate {
func willOpen(expandableButtonView: ExpandableButtonView) {}
func didOpen(expandableButtonView: ExpandableButtonView) {}
func willClose(expandableButtonView: ExpandableButtonView) {}
func didClose(expandableButtonView: ExpandableButtonView) {}
}

public class ExpandableButtonView: UIView {

public enum Direction {
Expand Down Expand Up @@ -58,6 +72,9 @@ public class ExpandableButtonView: UIView {
public var closeOnAction: Bool = false
public var isHapticFeedback = true

// delegate
public var delegate: ExpandableButtonDelegate?

// arrow

public var arrowInsets: UIEdgeInsets {
Expand Down Expand Up @@ -89,6 +106,7 @@ public class ExpandableButtonView: UIView {
public var separatorInset: CGFloat = 8 { didSet { reloadSeparatorFrame() } }
public var separatorWidth: CGFloat = 1 { didSet { reloadSeparatorFrame() } }


// MARK: - Private properties

private var firstLayout = true
Expand All @@ -104,9 +122,9 @@ public class ExpandableButtonView: UIView {
}

required public init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }

// MARK: - Overrides

public override var frame: CGRect { didSet { setupFrames() } }
public override var backgroundColor: UIColor? { didSet { arrowButton.backgroundColor = backgroundColor } }

Expand All @@ -126,9 +144,9 @@ public class ExpandableButtonView: UIView {
// MARK: - Public

public func open() {

guard state == .closed else { return }

self.delegate?.willOpen(expandableButtonView: self)

state = .animating
showOpenArrow()

Expand All @@ -141,13 +159,14 @@ public class ExpandableButtonView: UIView {
if $0 {
self.state = .opened
self.impactHapticFeedback()
self.delegate?.didOpen(expandableButtonView: self)
}
}
}

public func close() {

guard state == .opened else { return }
self.delegate?.willClose(expandableButtonView: self)

state = .animating

Expand All @@ -167,6 +186,7 @@ public class ExpandableButtonView: UIView {
self.itemsButtons.forEach { $0.isHidden = true }
self.state = .closed
self.impactHapticFeedback()
self.delegate?.didClose(expandableButtonView: self)
}
}

Expand All @@ -182,7 +202,7 @@ public class ExpandableButtonView: UIView {

arrowButton = ArrowButton()
arrowButton.actionBlock = { [weak self] in

guard let state = self?.state else { return }

switch state {
Expand Down Expand Up @@ -399,7 +419,6 @@ public class ExpandableButtonView: UIView {
}

private func close(with direction: Direction) {

switch direction {
case .up:
let itemsHeight = itemsButtons.reduce(0, { $0 + $1.frame.height })
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>ExpandableButtonExample.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>