Created
July 20, 2020 21:43
-
-
Save CodeSlicing/e18235069c5f819df97a1b5b5cef63a5 to your computer and use it in GitHub Desktop.
View showing how to use GeometryReader with a greedy view. A companion file to CodeSlicing: CoreConcepts in SwiftUI - GeometryReader
This file contains hidden or 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 | |
import PureSwiftUI | |
private let size: CGFloat = 200 | |
private let numLayers = 20 | |
private let stepSize = 25 | |
struct GeometryReaderGreedyViewDemo: View { | |
@State private var animating = false | |
var body: some View { | |
ZStack { | |
ForEach(0..<numLayers) { index in | |
ZStack { | |
PyramidLayer() | |
.rotateIf(self.animating, 90.degrees) | |
.shadow(5) | |
.frame(self.sizeForIndex(index)) | |
.animation(Animation | |
.easeInOut(duration: 2) | |
.repeatForever() | |
.delay(self.delayForIndex(index))) | |
} | |
} | |
.onAppear { | |
self.animating = true | |
} | |
} | |
.padding(100) | |
.drawingGroup() | |
} | |
func delayForIndex(_ index: Int) -> Double { | |
Double(numLayers - index) * 0.2 | |
} | |
func sizeForIndex(_ index: Int) -> CGFloat { | |
CGFloat((numLayers - index) * stepSize) | |
} | |
} | |
private let gradient = LinearGradient([Color.red, Color.yellow], to: .bottomTrailing) | |
private struct PyramidLayer: View { | |
var body: some View { | |
GeometryReader { (geo: GeometryProxy) in | |
RoundedRectangle(geo.widthScaled(0.2)) | |
.fill(gradient) | |
} | |
} | |
} | |
struct GeometryReaderDemo_Previews: PreviewProvider { | |
struct GeometryReaderDemo_Harness: View { | |
var body: some View { | |
GeometryReaderGreedyViewDemo() | |
} | |
} | |
static var previews: some View { | |
GeometryReaderDemo_Harness() | |
.previewDevice(.iPad_Air_2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment