Implicit Alignment
ๅจ HStack, VStack, ZStack ็ญ container ๅ ง็ view ้ฝๆ้ ่จญ็ alignment guide๏ผๅฆๆ view ๆฌ่บซๆฒๆ็ดๆฅไฝฟ็จ .alignmentGuide() ไพ่จญๅฎ๏ผ้ฃ้บผ view ๅฐฑๆไฝฟ็จ container ๆๆๅฎ็ alignmenet ไพๅฐ้ฝใ
import SwiftUI
/// โญ๏ธ ่ช่จๆฐดๅนณๆนๅ็ๅฐ้ฝ็ท `.firstView`
extension HorizontalAlignment {
private struct FirstViewAlignment: AlignmentID {
static func defaultValue(in dim: ViewDimensions) -> CGFloat {
dim[.leading]
}
}
static let firstView = HorizontalAlignment(
FirstViewAlignment.self
)
}
struct ImplicitAlign: View {
@State private var alignment: HorizontalAlignment = .leading
@State private var selection: String = "leading"
let keys = ["leading", "center", "trailing"]
let alignments: [String:HorizontalAlignment] = [
"leading": .leading, "center": .center, "trailing": .trailing
]
var body: some View {
VStack {
labelViews
picker
}.frame(maxHeight: .infinity).background(Color.gray)
}
/// vertical line
var line: some View {
VLine(color: .yellow).shadow(radius: 2)
}
/// segmented control
var picker: some View {
Picker("Align", selection: $selection) {
ForEach(0..<3){ Text(keys[$0]).tag(keys[$0]) }
}
.pickerStyle(.segmented).shadow(radius: 2)
.padding()
// โญ๏ธ animate the change when `selection` changes
.onChange(of: selection) { newSelection in
withAnimation(.easeOut(duration: 1)) {
self.alignment = alignments[newSelection]!
}
}
}
var labelViews: some View {
// VStack ๅ
ง้จไปฅ `alignment` ๅฐ้ฝ
VStack(alignment: alignment) {
LabelView(title: "Implicit", color: .red)
// โญ๏ธ ๅฐ `.firstView` ็ๅฐ้ฝ็ท่ชฟๆดๆ่ท `alignment` ไธๆจฃ
.alignmentGuide(.firstView) { $0[alignment] }
LabelView(title: "Explicit 1", color: .green)
.alignmentGuide(.leading) { _ in 30 }
.alignmentGuide(HorizontalAlignment.center) { _ in 30 }
.alignmentGuide(.trailing) { _ in 90 }
LabelView(title: "Explicit 2", color: .blue)
.alignmentGuide(.leading) { _ in 90 }
.alignmentGuide(HorizontalAlignment.center){ _ in 30 }
.alignmentGuide(.trailing) { _ in 30 }
}.padding().overlay(line, alignment: Alignment(
// โญ๏ธ ๅ็ด็ท `line` ๅฐ้ฝ `.firstView`
horizontal: .firstView, vertical: .center
))
}
}
struct LabelView: View {
let title: String
let color: Color
var body: some View {
Text(title)
.font(.title)
.foregroundColor(.white)
.padding(10)
.frame(width: 200, height: 40)
.background(LinearGradient(
gradient: Gradient(colors: [color, .black]),
startPoint: UnitPoint(x: 0, y: 0),
endPoint: UnitPoint(x: 2, y: 1)
))
.shadow(radius: 3)
}
}
SwiftUI โฉ View โฉ Input and Events
onChange(of:perform:) โญ๏ธ - fires an action when a specific value changes.
SwiftUI Lab โฉ Alignment Guides in SwiftUI
ๅฝผๅพๆฝ โฉ SwiftUI ๅตๆธฌๅ งๅฎนๆน่ฎ็ onChange modifier
Hacking with Swift โฉ How to run some code when state changes using onChange()
Developer โฉ @State + didSet Property Observer
Last updated