41 lines
1.5 KiB
Swift
41 lines
1.5 KiB
Swift
//
|
|
// ButtonGroupCollectionViewCell.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 12/15/22.
|
|
//
|
|
|
|
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
|
|
}
|
|
|
|
}
|