Overview
After going over the mechanics of how the course works, this first lecture dives right into creating an iOS application (a card-matching game called Memorize). The Xcode development environment is used to demonstrate the basics of SwiftUI’s declarative approach to composing user-interfaces.
➜ Watch Video
Lecture
Learning
SwiftUI 是基于函数式编程
import SwiftUIstruct ContentView: View {var body: some View {Text("Hello, World!")}}
some View一个有趣的属性类型,只要它的行为类似于 View,就可以是任何类型,任何结构(is any type, any struct, as long as it behaves likes a View)。当做乐高,UI 组件都是积木,积木可以合成新组件,越来越大。- var body 是计算属性,不写入内存
struct CardView: View {
var isSelected: Bool = false
var body: some View {
ZStack {
if isSelected {
RoundedRectangle(cornerRadius: 10.0)
.foregroundColor(Color.white)
RoundedRectangle(cornerRadius: 10.0)
.stroke(lineWidth: 3.0)
Text("👻").font(Font.largeTitle)
} else {
RoundedRectangle(cornerRadius: 10.0)
// .foregroundColor(Color.orange)
}
}
.foregroundColor(Color.orange)
}
}
foregroundColor外层会想内层传递颜色属性- 虽然
ZStackHStackVStack都能设置间距,但尽量用默认的间距
