created new cell for button group
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
fd8e68f1c3
commit
04bbb8948a
@ -84,7 +84,7 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
|
|||||||
$0.translatesAutoresizingMaskIntoConstraints = false
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
||||||
$0.dataSource = self
|
$0.dataSource = self
|
||||||
$0.delegate = self
|
$0.delegate = self
|
||||||
$0.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionViewCell")
|
$0.register(ButtonCollectionViewCell.self, forCellWithReuseIdentifier: "collectionViewCell")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -166,11 +166,13 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
|
|||||||
|
|
||||||
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
||||||
let button = buttons[indexPath.row]
|
let button = buttons[indexPath.row]
|
||||||
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)
|
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? ButtonCollectionViewCell else { return UICollectionViewCell() }
|
||||||
cell.subviews.forEach { $0.removeFromSuperview() }
|
cell.subviews.forEach { $0.removeFromSuperview() }
|
||||||
cell.addSubview(button)
|
cell.addSubview(button)
|
||||||
|
cell.buttonable = button
|
||||||
button.pinLeading()
|
button.pinLeading()
|
||||||
button.pinTrailing()
|
button.pinTrailing()
|
||||||
|
cell.buttonIntrinsicContentSize = button.intrinsicContentSize
|
||||||
button.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
|
button.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
|
||||||
return cell
|
return cell
|
||||||
}
|
}
|
||||||
@ -193,3 +195,31 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ButtonCollectionViewCell: UICollectionViewCell {
|
||||||
|
|
||||||
|
var buttonIntrinsicContentSize: CGSize = .zero
|
||||||
|
var buttonable: Buttonable?
|
||||||
|
|
||||||
|
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
|
||||||
|
// 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: buttonIntrinsicContentSize.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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user