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

点击查看【bilibili】

Lecture

Lecture 1.pdf

Learning

SwiftUI 是基于函数式编程

  1. import SwiftUI
  2. struct ContentView: View {
  3. var body: some View {
  4. Text("Hello, World!")
  5. }
  6. }
  • 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 外层会想内层传递颜色属性
  • 虽然 ZStack HStack VStack 都能设置间距,但尽量用默认的间距