From 3e8461aef6c0e184750d806c56a9d471b2c78169 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 2 May 2023 12:12:40 -0500 Subject: [PATCH 1/2] updated for buttongroup Signed-off-by: Matt Bruce --- .../Buttons/ButtonGroup/ButtonGroup.swift | 6 ---- .../ButtonGroupCollectionViewCell.swift | 32 ++++--------------- VDS/Extensions/UIView.swift | 15 +++++---- 3 files changed, 15 insertions(+), 38 deletions(-) diff --git a/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift b/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift index e1dc6d18..4b162465 100644 --- a/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift +++ b/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift @@ -71,11 +71,6 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- - private let lineSpacing: CGFloat = 12.0 - private let itemSpacing: CGFloat = 16.0 - private let estimatedCellHeight: CGFloat = 40.0 - private let estimatedCellWidth: CGFloat = 150.0 - fileprivate lazy var positionLayout = ButtonGroupPositionLayout().with { $0.position = .center $0.delegate = self @@ -173,7 +168,6 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? ButtonGroupCollectionViewCell else { return UICollectionViewCell() } cell.subviews.forEach { $0.removeFromSuperview() } cell.addSubview(button) - cell.buttonable = button button.pinToSuperView() return cell } diff --git a/VDS/Components/Buttons/ButtonGroup/ButtonGroupCollectionViewCell.swift b/VDS/Components/Buttons/ButtonGroup/ButtonGroupCollectionViewCell.swift index 13e56a8e..7e63d749 100644 --- a/VDS/Components/Buttons/ButtonGroup/ButtonGroupCollectionViewCell.swift +++ b/VDS/Components/Buttons/ButtonGroup/ButtonGroupCollectionViewCell.swift @@ -9,32 +9,12 @@ import Foundation import UIKit -/// Cell is needed since the buttonable hitArea "can" be outside of it's container rectangle -public class ButtonGroupCollectionViewCell: UICollectionViewCell { - - var buttonable: Buttonable? - - open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { - guard let buttonable else { return nil } - - // Create a minimumHitArea variable with a value that represents the minimum size of the hit area you want to create for the button. - let minimumHitArea = CGSize(width: buttonable.intrinsicContentSize.width, height: 44) - - // Create a new hitFrame variable that is the same size as the minimumHitArea variable, but is centered on the button's frame. - let hitFrame = CGRect( - x: self.bounds.midX - minimumHitArea.width / 2, - y: self.bounds.midY - minimumHitArea.height / 2, - width: minimumHitArea.width, - height: minimumHitArea.height - ) - - // If the point that was passed to the hitTest(_:with:) method is within the hitFrame, return the button itself. This will cause the button to handle the touch event. - if hitFrame.contains(point) { - return buttonable - } - - // If the point is not within the hitFrame, return nil. This will cause the touch event to be handled by another view. - return nil +public class ButtonGroupCollectionViewCell: UICollectionViewCell {} + +extension ButtonGroupCollectionViewCell: AppleGuidlinesTouchable { + + override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool { + Self.acceptablyOutsideBounds(point: point, bounds: bounds) } } diff --git a/VDS/Extensions/UIView.swift b/VDS/Extensions/UIView.swift index 74dfa59f..818e8d18 100644 --- a/VDS/Extensions/UIView.swift +++ b/VDS/Extensions/UIView.swift @@ -120,7 +120,16 @@ extension UIView { extension UIView { + private func removeDebugBorder() { + layer.sublayers?.forEach({ layer in + if layer.name?.hasPrefix("debug") ?? false { + layer.removeFromSuperlayer() + } + }) + } + public func debugBorder(show shouldShow: Bool = true, color: UIColor = .red) { + removeDebugBorder() if shouldShow { let borderLayer = CALayer() borderLayer.name = "debugAreaLayer" @@ -147,12 +156,6 @@ extension UIView { touchLayer.bounds = bounds layer.addSublayer(touchLayer) } - } else { - layer.sublayers?.forEach({ layer in - if layer.name?.hasPrefix("debug") ?? false { - layer.removeFromSuperlayer() - } - }) } } } From 29a36ee7d4d95ea4ba4d0439a58b13c0e9f2f928 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 2 May 2023 13:22:40 -0500 Subject: [PATCH 2/2] added methods for showing button cell bounds Signed-off-by: Matt Bruce --- .../Buttons/ButtonGroup/ButtonGroup.swift | 6 +- VDS/Extensions/UIView.swift | 74 ++++++++++++------- 2 files changed, 51 insertions(+), 29 deletions(-) diff --git a/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift b/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift index 4b162465..ff0b6152 100644 --- a/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift +++ b/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift @@ -169,6 +169,11 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega cell.subviews.forEach { $0.removeFromSuperview() } cell.addSubview(button) button.pinToSuperView() + if hasDebugBorder { + cell.addDebugBorder() + } else { + cell.removeDebugBorder() + } return cell } @@ -188,5 +193,4 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega buttons[indexPath.row] } - } diff --git a/VDS/Extensions/UIView.swift b/VDS/Extensions/UIView.swift index 818e8d18..e4e1d0ad 100644 --- a/VDS/Extensions/UIView.swift +++ b/VDS/Extensions/UIView.swift @@ -118,9 +118,9 @@ extension UIView { } - extension UIView { - private func removeDebugBorder() { + + internal func removeDebugBorder() { layer.sublayers?.forEach({ layer in if layer.name?.hasPrefix("debug") ?? false { layer.removeFromSuperlayer() @@ -128,34 +128,52 @@ extension UIView { }) } - public func debugBorder(show shouldShow: Bool = true, color: UIColor = .red) { + internal func addDebugBorder(color: UIColor = .red) { + //ensure you remove existing removeDebugBorder() - if shouldShow { - let borderLayer = CALayer() - borderLayer.name = "debugAreaLayer" - borderLayer.frame = bounds - borderLayer.bounds = bounds - borderLayer.borderWidth = VDSFormControls.widthBorder - borderLayer.borderColor = color.cgColor - layer.addSublayer(borderLayer) + + //add bounds border + let borderLayer = CALayer() + borderLayer.name = "debugAreaLayer" + borderLayer.frame = bounds + borderLayer.bounds = bounds + borderLayer.borderWidth = VDSFormControls.widthBorder + borderLayer.borderColor = color.cgColor + layer.addSublayer(borderLayer) + + //add touchborder if applicable + if type(of: self) is AppleGuidlinesTouchable.Type { + let faultToleranceX: CGFloat = max((45 - bounds.size.width) / 2.0, 0) + let faultToleranceY: CGFloat = max((45 - bounds.size.height) / 2.0, 0) - if type(of: self) is AppleGuidlinesTouchable.Type { - let faultToleranceX: CGFloat = max((45 - bounds.size.width) / 2.0, 0) - let faultToleranceY: CGFloat = max((45 - bounds.size.height) / 2.0, 0) - - let touchableAreaPath = UIBezierPath(rect: bounds.insetBy(dx: -faultToleranceX, dy: -faultToleranceY)) - let touchLayer = CAShapeLayer() - touchLayer.path = touchableAreaPath.cgPath - touchLayer.strokeColor = color.cgColor - touchLayer.fillColor = UIColor.clear.cgColor - touchLayer.lineWidth = VDSFormControls.widthBorder - touchLayer.opacity = 1.0 - touchLayer.name = "debugTouchableAreaLayer" - touchLayer.zPosition = 100 - touchLayer.frame = bounds - touchLayer.bounds = bounds - layer.addSublayer(touchLayer) - } + let touchableAreaPath = UIBezierPath(rect: bounds.insetBy(dx: -faultToleranceX, dy: -faultToleranceY)) + let touchLayer = CAShapeLayer() + touchLayer.path = touchableAreaPath.cgPath + touchLayer.strokeColor = color.cgColor + touchLayer.fillColor = UIColor.clear.cgColor + touchLayer.lineWidth = VDSFormControls.widthBorder + touchLayer.opacity = 1.0 + touchLayer.name = "debugTouchableAreaLayer" + touchLayer.zPosition = 100 + touchLayer.frame = bounds + touchLayer.bounds = bounds + layer.addSublayer(touchLayer) + } + } + + public var hasDebugBorder: Bool { + guard let layers = layer.sublayers else { return false } + return layers.compactMap{$0.name}.filter{$0.hasPrefix("debug")}.count > 0 + } + + public func debugBorder(show shouldShow: Bool = true, color: UIColor = .red) { + if shouldShow { + addDebugBorder(color: color) + } else { + removeDebugBorder() + } + if let view = self as? Handlerable { + view.updateView() } } }