Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Last active February 12, 2022 20:46
Show Gist options
  • Save CodeSlicing/72748efb29c72e29a3af4944f0ce969e to your computer and use it in GitHub Desktop.
Save CodeSlicing/72748efb29c72e29a3af4944f0ce969e to your computer and use it in GitHub Desktop.
Native source code for CodeSlicing episode exploring overlay / background modifiers vs ZStack - demo-02
//
// OverlayVsZStackDemo02Native.swift
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright © 2021 Adam Fordyce. All rights reserved.
//
import SwiftUI
private func Square(_ color: Color, _ sideLength: CGFloat = 100) -> some View {
Rectangle()
.fill(color)
.frame(width: sideLength, height: sideLength)
}
struct OverlayVsStackDemo02Native: View {
@State private var size = CGSize(width: 0, height: 0)
var body: some View {
let centerSquare = Square(.yellow)
let bigSquare = Square(.white.opacity(0.5), 200)
let littleSquare = Square(.blue, 50)
VStack {
createPattern {
centerSquare
.background(bigSquare)
}
createPattern {
ZStack(alignment: .topLeading) {
bigSquare
.frame(width: size.width, height: size.height)
ZStack(alignment: .bottomTrailing) {
centerSquare
.geometryReaderNative { geo in
size = geo.size
}
littleSquare
}
littleSquare
}
.frame(width: size.width, height: size.height)
}
}
}
private func createPattern<Content: View>(@ViewBuilder content: () -> Content) -> some View {
let redSquare = Square(.red)
return VStack {
redSquare
HStack {
redSquare
content()
redSquare
}
redSquare
}
}
}
// added the implementation for geometryReader here. Called it native so it doesn't
// interfere with the PureSwiftUI version if you have that installed
private extension View {
func geometryReaderNative(_ geoCallback: @escaping (GeometryProxy) -> ()) -> some View {
geometryReaderNative(id: 1, geoCallback)
}
func geometryReaderNative<T: Hashable>(id: T, _ geoCallback: @escaping (GeometryProxy) -> ()) -> some View {
overlay(GeometryReader { (geo: GeometryProxy) in
Color.clear.onAppear {
geoCallback(geo)
}
.id(id)
})
}
}
struct OverlayVsStackDemo02Native_Previews: PreviewProvider {
struct OverlayVsStackDemo02Native_Harness: View {
var body: some View {
OverlayVsStackDemo02Native()
.padding(20)
.foregroundColor(.white)
.background(Color(white: 0.1))
}
}
static var previews: some View {
OverlayVsStackDemo02Native_Harness()
.previewLayout(.sizeThatFits)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment