import SwiftUI
typealias HA = HorizontalAlignment
typealias VA = VerticalAlignment
let w: CGFloat = 100
let h: CGFloat = 50
let r1 = Rectangle().padding(1).frame(w, h).foregroundColor(.pink)
let r2 = Rectangle().padding(1).frame(h, w).foregroundColor(.blue)
struct MyRects: View {
var body: some View {
ZStack {
r1
.alignmentGuide(HA.center) { $0[.leading] }
.alignmentGuide(VA.center) { $0[.bottom] }
r1
.alignmentGuide(HA.center) { $0[.trailing] }
.alignmentGuide(VA.center) { $0[.top] }
r2
.alignmentGuide(HA.center) { $0[.trailing] }
.alignmentGuide(VA.center) { $0[.bottom] }
r2
.alignmentGuide(HA.center) { $0[.leading] }
.alignmentGuide(VA.center) { $0[.top] }
}// VStack (container)
.aspectRatio(1, contentMode: .fit)
}
}
// live view
struct ContentView: View {
var body: some View {
HStack {
MyRects() // ⭐️ 不同圖層,影子會有層次感
MyRects()
.compositingGroup() // ⭐️ 變為同一圖層
}// HStack
.padding()
.shadow(color: .black, radius: 4, x: 4, y: 4)
.background(Color.gray)
}
}