From 1e44d9738dc672e6af61a33204dea9a33dcb34c0 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 15 Dec 2022 11:46:35 -0600 Subject: [PATCH] added hitArea Signed-off-by: Matt Bruce --- .../Buttons/Button/ButtonBase.swift | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/VDS/Components/Buttons/Button/ButtonBase.swift b/VDS/Components/Buttons/Button/ButtonBase.swift index 005c542a..cb69771f 100644 --- a/VDS/Components/Buttons/Button/ButtonBase.swift +++ b/VDS/Components/Buttons/Button/ButtonBase.swift @@ -19,7 +19,11 @@ public protocol Buttonable: UIControl, Surfaceable, Disabling { @objc(VDSButtonBase) open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettable { - + //-------------------------------------------------- + // MARK: - Configuration Properties + //-------------------------------------------------- + private let hitAreaHeight = 44.0 + //-------------------------------------------------- // MARK: - Combine Properties //-------------------------------------------------- @@ -194,4 +198,28 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab setAttributedTitle(mutableText, for: .normal) setAttributedTitle(mutableText, for: .highlighted) } + + open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { + let size = intrinsicContentSize + // 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: size.width, height: hitAreaHeight) + + // 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 self + } + + // If the point is not within the hitFrame, return nil. This will cause the touch event to be handled by another view. + return nil + } + } +