0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【SwiftUI】公式 Tutorials「 Animating Views and Transitions」の解説Part.2

Posted at

この記事は何?

Section1「Add Hiking Data to the App」で作成するビューのコードを読んでみました。

環境

macOS 11.2.1
Xcode 12.4
Swift 5.3

HikeDetailビュー

struct HikeDetail: View {
    let hike: Hike
    @State var dataToShow = \Hike.Observation.elevation

    var buttons = [
        ("Elevation", \Hike.Observation.elevation),
        ("Heart Rate", \Hike.Observation.heartRate),
        ("Pace", \Hike.Observation.pace)
    ]

    var body: some View {
        return VStack {
            HikeGraph(hike: hike, path: dataToShow)
                .frame(height: 200)

            HStack(spacing: 25) {
                ForEach(buttons, id: \.0) { value in
                    Button(action: {
                        self.dataToShow = value.1
                    }) {
                        Text(value.0)
                            .font(.system(size: 15))
                            .foregroundColor(value.1 == self.dataToShow ? Color.gray
                                                                        : Color.accentColor)
                            .animation(nil)
                    }
                }
            }
        }
    }
}

HikeDetailビューの外観
スクリーンショット 2021-02-16 1.33.54.png

HikeDetailを構成するビュー階層
スクリーンショット 2021-02-16 1.39.12.png

状態変数detailToShowの挙動

ForEach(buttons, id: \.0) { value in
    Button(action: {
        self.dataToShow = value.1
    }) {
    Text(value.0)
        .foregroundColor(value.1 == self.dataToShow ? Color.gray : Color.accentColor)
    }                
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?