Created
August 15, 2020 20:38
-
-
Save CastIrony/ceb12947ff42ec60875d6ef3a27d3092 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct RegularEventCell: View { | |
let color: Color | |
let title: String | |
let description: String? | |
var body: some View { | |
HStack { | |
color.frame(width: 3) | |
VStack (alignment: .leading, spacing: 0) { | |
Text(title) | |
.font(.caption) | |
if let desc = description | |
{ | |
Text(desc) | |
.font(.caption2) | |
.foregroundColor(.secondary) | |
} | |
} | |
Spacer() | |
} | |
.padding([.leading, .trailing], 10) | |
.padding([.bottom, .top], 5) | |
.readFrame() | |
} | |
} | |
extension View { | |
func readFrame() -> some View { | |
background( | |
GeometryReader { geometryProxy in | |
Color.clear.preference(key: FramePreferenceKey.self, value: [geometryProxy.frame(in: .global)]) | |
} | |
) | |
} | |
} | |
private struct FramePreferenceKey: PreferenceKey { | |
static var defaultValue: [CGRect] = [] | |
static func reduce(value: inout [CGRect], nextValue: () -> [CGRect]) | |
{ | |
value.append(contentsOf: nextValue()) | |
} | |
} | |
struct ModelRow: Identifiable { | |
let id: String = NSUUID().uuidString | |
let title: String | |
let description: String? | |
} | |
let rows = [ModelRow(title: "One", description: "First"), | |
ModelRow(title: "Two", description: nil), | |
ModelRow(title: "Three", description: "Third"), | |
ModelRow(title: "Four", description: nil), | |
ModelRow(title: "Five", description: "Fifth"), | |
ModelRow(title: "Six", description: nil)] | |
struct ContentView : View { | |
@State var rowFrames: [CGRect] = [] | |
var body: some View { | |
GeometryReader { geometry in // causes VStack to be same size as widget | |
VStack(alignment: .leading, spacing: 1) { | |
ForEach(0 ..< rows.count) { rowIndex in | |
Group { | |
RegularEventCell(color: .red, title: rows[rowIndex].title, description: rows[rowIndex].description) | |
Divider() | |
} | |
.opacity(rowIndex < rowFrames.count && rowFrames[rowIndex].maxY < geometry.size.height ? 1 : 0) | |
} | |
Spacer() | |
} | |
} | |
.onPreferenceChange(FramePreferenceKey.self, perform: { value in | |
rowFrames = value | |
dump(value) | |
}) | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView().previewLayout(.fixed(width: 200, height: 170)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment