DMHoneyCombGrid
演示应用程序以显示如何使用 autolayout 动态创建任何每边单元格数量的横向六边形形状的 HoneyCombGrid
2x2 HoneyCombGrid 的 iOS UIView 示例,具有随机样本背景颜色
示例
要运行示例项目,请先克隆仓库,然后在 Example 目录中运行 pod install
安装
DMHoneyCombGrid 通过 CocoaPods 提供。要安装它,只需简单地将以下行添加到您的 Podfile 中
pod 'DMHoneyCombGrid'
使用方法
只需将其作为视图添加,并根据您喜欢的设置一些约束,最好将您自己设置为目标(HoneyCombGridDelegate
)并实现 func honeyCombTapped(on cell: UIView)
以接收单个单元格的点击事件
import UIKit
import DMHoneyCombGrid
class ViewController: UIViewController, HoneyCombGridDelegate {
let containerView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
containerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
containerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
containerView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
containerView.backgroundColor = UIColor(red: 254.0 / 255.0, green: 213.0 / 255.0, blue: 151.0 / 255.0, alpha: 1.0)
loadGrid()
}
private func loadGrid() {
let hexGrid = HoneyCombGrid(cellPerSide: 2)
hexGrid.delegate = self
hexGrid.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(hexGrid)
// Example of laying it out with specific constraints 300w/300h
hexGrid.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
hexGrid.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true
hexGrid.widthAnchor.constraint(equalToConstant: 300).isActive = true
hexGrid.heightAnchor.constraint(equalToConstant: 300).isActive = true
}
func honeyCombTapped(on cell: UIView) {
print("Tapped on \(cell.tag)")
}
}
作者
Denis Mullaraj, [email protected]
许可证
DMHoneyCombGrid遵循MIT许可证。有关更多信息,请参阅LICENSE文件。