Skip to content

Instantly share code, notes, and snippets.

@CodeSlicing
Last active February 12, 2022 20:46
Show Gist options
  • Save CodeSlicing/d5bd8cf96354b1de082088108416965c to your computer and use it in GitHub Desktop.
Save CodeSlicing/d5bd8cf96354b1de082088108416965c to your computer and use it in GitHub Desktop.
Source code for CodeSlicing episode exploring overlay / background modifiers vs ZStack - demo-01
//
// OverlayVsZStackDemo01.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
import PureSwiftUI
private func Square(_ color: Color, _ sideLength: CGFloat = 100) -> some View {
Rectangle()
.fill(color)
.frame(sideLength)
}
struct OverlayVsZStackDemo01: View {
var body: some View {
let offset: CGFloat = 20
let blueSquare = Square(.blue)
let whiteSquare = Square(.white).offset(offset, offset)
let yellowSquare = Square(.yellow).offset(offset, -offset)
let greenSquare = Square(.green).offset(-offset, -offset)
let redSquare = Square(.red).offset(-offset, offset)
VStack(spacing: 0) {
blueSquare
.overlay(whiteSquare)
.background(yellowSquare)
.background(greenSquare)
.overlay(redSquare)
ZStack {
greenSquare
yellowSquare
blueSquare
whiteSquare
redSquare
}
}
}
}
struct OverlayVsZStackDemo01_Previews: PreviewProvider {
struct OverlayVsZStackDemo01_Harness: View {
var body: some View {
OverlayVsZStackDemo01()
.padding(100)
.foregroundColor(.white)
.backgroundColor(.white(0.1))
}
}
static var previews: some View {
OverlayVsZStackDemo01_Harness()
.previewSizeThatFits()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment