Stanford cs193p SwiftUI入门
本文是斯坦福大学 cs193p 公开课程第01集的相关笔记。
cs193p 课程介绍:
The lectures for the Spring 2023 version of Stanford University’s course CS193p (Developing Applications for iOS using SwiftUI) were given in person but, unfortunately, were not video recorded. However, we did capture the laptop screen of the presentations and demos as well as the associated audio. You can watch these screen captures using the links below. You’ll also find links to supporting material that was distributed to students during the quarter (homework, demo code, etc.).
cs193p 课程网址: https://cs193p.sites.stanford.edu/2023
创建新的iOS项目
选择 File -> New -> Project
“Hello World” 代码
完整的 “Hello World” 代码
1
2
3
4
5
6
7
8
9
10
11
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
}
struct 和 View
1
2
3
4
struct ContentView: View {
...
...
}
struct代表结构体,它是SwiftUI的核心。它可以包含变量和函数。Swift不是面向对象的编程语言,因此struct不是class。它是函数式编程。ContentView是结构体的名称。View意味着ContentView结构体像一个View。
var
1
2
3
4
5
6
struct ContentView: View {
var body: some View {
...
...
}
}
var是一个变量的关键字,body是变量名。其他变量可能如下所示:
1
2
var i: Int
var s: String
计算属性 (Computed Property)
让我们看看 body 中的内容:
1
2
3
4
5
6
7
8
9
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
var body是一个属性,body变量的值不会存储在某个地方,而是在每次程序运行时都会计算一次。在 “Hello World” 代码中,它计算的是如下部分:
1
2
3
4
5
6
7
8
9
{
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
some View 与 String 或 Int不同。它一定是某种类型的 View,但可以是不同类型的 View,只要它是一个 View 即可。
VStack
1
2
3
4
5
6
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
creating instance of structs:像图像结构体
Image(systemName: "globe");或文本结构体Text("Hello, world!")。named parameters:
systemName是一个参数名。Parameter defaults:还可以指定其他参数,但我们可以使用默认值。
这个
VStack有两个结构体,它们都像一个View一样。它包括Image和Text结构体。但是,VStack本身也是一个像View一样的结构体。我们可以为VStack提供参数,例如:
1
2
3
4
5
6
VStack(alignment: .leading, spacing: 20) {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
- 那么
VStack内部的整个内容呢?- 这是
VStack的一个参数,完整内容应该如下所示:
- 这是
1
2
3
4
5
6
VStack(content: {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
})
VStack也被称为 @ViewBuilder。@ViewBuilder 可以将视图列表(Image、Text)转换为 TupleView。
View modifier
1
2
3
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
.imageScale和.foregroundStyle称为 View modifier。View modifier 将值返回给.imageScale,然后.imageScale也返回给.foregroundStyle。因此,它们可以连接在一起。
View modifier 的作用域
视图修改器的作用域很重要。如果作用域不同,将显示不同。
▲ 作用域 1
▲ 作用域 2
开始创建卡片
▲ 创建一个带有白色背景的单张卡片
▲ 创建4张卡片
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
import SwiftUI
struct ContentView: View {
var body: some View {
HStack {
CardView(isFaceUp: true)
CardView()
CardView()
CardView()
}
.foregroundColor(.orange)
.padding()
}
}
struct CardView: View {
var isFaceUp: Bool = false
var body: some View {
ZStack(content: {
if isFaceUp {
RoundedRectangle(cornerRadius: 12)
.foregroundColor(.white)
RoundedRectangle(cornerRadius: 12)
.strokeBorder(lineWidth: 2)
Text("👻").font(.largeTitle)
} else {
RoundedRectangle(cornerRadius: 12)
}
})
}
}
#Preview {
ContentView()
}
▲ 创建4张卡片,并带有一个可以控制卡片正反面的参数
注意:每个 变量(var) 必须有一个值,但我们可以设置一个默认值。




