|
| 1 | +// |
| 2 | +// WrapStackLayerDemoViewController.swift |
| 3 | +// Demo |
| 4 | +// |
| 5 | +// Created by i on 2023/6/12. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | +import StackKit |
| 10 | +import PinLayout |
| 11 | + |
| 12 | +extension WrapStackLayer: SizeCalculable { } |
| 13 | + |
| 14 | +class WrapStackLayerDemoViewController: UIViewController { |
| 15 | + |
| 16 | + let fixedWrapStackLayer = WrapStackLayer( |
| 17 | + itemSize: .fixed(CGSize(width: 30, height: 30)), |
| 18 | + contentInsets: UIEdgeInsets(top: 20, left: 14, bottom: 20, right: 14), |
| 19 | + itemSpacing: 4, |
| 20 | + lineSpacing: 10 |
| 21 | + ) |
| 22 | + |
| 23 | + let adaptiveWrapStackLayer = WrapStackLayer( |
| 24 | + itemSize: .adaptive(column: 6), |
| 25 | + contentInsets: UIEdgeInsets(top: 20, left: 14, bottom: 20, right: 14), |
| 26 | + itemSpacing: 10, |
| 27 | + lineSpacing: 4 |
| 28 | + ) |
| 29 | + |
| 30 | + let autoWrapStackLayer = WrapStackLayer( |
| 31 | + itemSize: .auto, |
| 32 | + contentInsets: UIEdgeInsets(top: 20, left: 14, bottom: 20, right: 14), |
| 33 | + itemSpacing: 10, |
| 34 | + lineSpacing: 10 |
| 35 | + ) |
| 36 | + |
| 37 | + override func viewDidLoad() { |
| 38 | + super.viewDidLoad() |
| 39 | + |
| 40 | + // Do any additional setup after loading the view. |
| 41 | + let widths: [CGFloat] = [20, 30, 50, 120, 40, 230, 50, 60, 10] |
| 42 | + fixedWrapStackLayer.addContent { |
| 43 | + for _ in (0 ... 20) { |
| 44 | + makeUIView(size: CGSize(width: widths.randomElement() ?? 10, height: 30)) |
| 45 | + } |
| 46 | + } |
| 47 | + view.layer.addSublayer(fixedWrapStackLayer) |
| 48 | + |
| 49 | + adaptiveWrapStackLayer.addContent { |
| 50 | + for _ in (0 ... 11) { |
| 51 | + makeUIView(size: CGSize(width: widths.randomElement() ?? 10, height: 30)) |
| 52 | + } |
| 53 | + } |
| 54 | + view.layer.addSublayer(adaptiveWrapStackLayer) |
| 55 | + |
| 56 | + autoWrapStackLayer.addContent { |
| 57 | + for _ in (0 ... 11) { |
| 58 | + makeUIView(size: CGSize(width: widths.randomElement() ?? 10, height: 30)) |
| 59 | + } |
| 60 | + } |
| 61 | + view.layer.addSublayer(autoWrapStackLayer) |
| 62 | + } |
| 63 | + |
| 64 | + private func makeUIView(size: CGSize) -> CALayer { |
| 65 | + let v = CALayer() |
| 66 | + let red: CGFloat = CGFloat((0 ... 255).randomElement() ?? 0) / 255 |
| 67 | + let green: CGFloat = CGFloat((0 ... 255).randomElement() ?? 0) / 255 |
| 68 | + let blue: CGFloat = CGFloat((0 ... 255).randomElement() ?? 0) / 255 |
| 69 | + v.backgroundColor = UIColor.init(red: red, green: green, blue: blue, alpha: 1).cgColor |
| 70 | + v.frame.size = size |
| 71 | + return v |
| 72 | + } |
| 73 | + |
| 74 | + override func viewDidLayoutSubviews() { |
| 75 | + super.viewDidLayoutSubviews() |
| 76 | + |
| 77 | + fixedWrapStackLayer.pin.top(120).maxWidth(220).sizeToFit(.width) |
| 78 | + adaptiveWrapStackLayer.pin.top(to: fixedWrapStackLayer.edge.bottom).marginTop(50).horizontally().sizeToFit(.width) |
| 79 | + autoWrapStackLayer.pin.top(to: adaptiveWrapStackLayer.edge.bottom).marginTop(50).horizontally().sizeToFit(.width) |
| 80 | + } |
| 81 | + |
| 82 | + /* |
| 83 | + // MARK: - Navigation |
| 84 | + |
| 85 | + // In a storyboard-based application, you will often want to do a little preparation before navigation |
| 86 | + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { |
| 87 | + // Get the new view controller using segue.destination. |
| 88 | + // Pass the selected object to the new view controller. |
| 89 | + } |
| 90 | + */ |
| 91 | + |
| 92 | +} |
0 commit comments