-
Notifications
You must be signed in to change notification settings - Fork 3
/
ContentView.swift
63 lines (53 loc) · 1.75 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// ContentView.swift
// P18I LayoutAndGeometry
//
// Created by Julian Moorhouse on 13/05/2021.
//
// Use an iPhone Pro Max 13.5 - to match Pauls settings
// see https://www.hackingwithswift.com/books/ios-swiftui/understanding-frames-and-coordinates-inside-geometryreader
// for explanation
// Want to know where this view is on the screen? Use the global space.
// Want to know where this view is relative to its parent? Use the local space.
// What to know where this view is relative to some other view? Use a custom space.
import SwiftUI
struct OuterView: View {
var body: some View {
VStack {
Text("Top")
InnerView()
.background(Color.green)
Text("Bottom")
}
}
}
struct InnerView: View {
var body: some View {
HStack {
Text("Left")
GeometryReader { geo in
Text("Center")
.background(Color.blue)
.onTapGesture {
print("Global center: \(geo.frame(in: .global).midX) x \(geo.frame(in: .global).midY)")
print("Custom center: \(geo.frame(in: .named("Custom")).midX) x \(geo.frame(in: .named("Custom")).midY)")
print("Local center: \(geo.frame(in: .local).midX) x \(geo.frame(in: .local).midY)")
}
}
.background(Color.orange)
Text("Right")
}
}
}
struct ContentView: View {
var body: some View {
OuterView()
.background(Color.red)
.coordinateSpace(name: "Custom")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}