Open3

SwiftUIのtutorialやってみた話

tiking76tiking76

SwiftUIが宣言的に書けると言われている部分

@main
struct LandmarksApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

ここのAppはprotocolで実装されている。
→ライフサイクルの都合でこうなっている。
具象型の概念とかを使ってるのな?
someについての参考資料
@mainはアプリのエントリーポイントになっていることを表している。
ここでは、ContentViewは別ファイルで作っている

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .padding()
    }
}

VStackとかHStackとかとか…


structで実装されている。

  • padding()
  • Spacer()
  • VStack(alignment: .leading)→左揃え
  • VStack(alignment: .center)→中央揃え
  • VStack(alignment: .trailing)→右揃え

Imageとか他のアセット

  • 画像を丸くしたい
struct CircleImage: View {
   var body: some View {
       Image("turtlerock")
           .clipShape(Circle())
   }
}
  • 枠線を付けたい
struct CircleImage: View {
    var body: some View {
        Image("turtlerock")
            .clipShape(Circle())
            .overlay(Circle().stroke(Color.gray, lineWidth: 4))
    }
}
  • 影をつける
struct CircleImage: View {
    var body: some View {
        Image("turtlerock")
            .clipShape(Circle())
            .overlay(Circle().stroke(Color.gray, lineWidth: 4))
            .shadow(radius: 7)
    }
}

MapKit

  • @Stateの意味
    →Structは値型なので、classのような運用方法ができないため@Stateを付けたプロパティを用いてる
    →@Stateを付与したプロパティはメモリ管理がSwiftUIフレームワークに委譲され、変更が可能となります。
    @Stateについての参考資料

  • LivePreview

    |>ボタンをクリック

Divider

  • 区切り線を引く
  • 引数なしのイニシャライザ

fontとかの指定

HStack {
    Text("Joshua Tree National Park")
     Spacer()
     Text("California")
 }
.font(.subheadline)
.foregroundColor(.secondary)

HStackとかで括ってあるとまとめて指定できる

tiking76tiking76

Hashable

Modelの作成

Create the Row View

Customize the Row Preview

Group

@frozen public struct Group<Content> {

    /// The type of content representing the body of this toolbar content.
    public typealias Body = Never
}

Text(Image("landmark"))

でもエラーは出ない

tiking76tiking76

View

protocol View

Viewプロトコルは、アプリのレイアウトに位置し、Configureビューに使用することを、デフォルトの実装とプロトコルのメソッドとして定義された修飾子の大規模なセットを提供します。