我正在嘗試重新創建與 Carrot Weather Layout 修改器相同的視圖,即按鈕頂部的輪播,按下該按鈕時,將打開當前顯示的專案的詳細視圖。
你會采取什么方法來實作這一目標?我嘗試使用 onTapesture 和 onDragGesture 來存盤附加到專案的值,但這似乎不起作用。
謝謝您的幫助!

uj5u.com熱心網友回復:
一種方法可能是使用 TabView 的 selection 屬性,并使用 .tag(index) 標記 TabView 內的每個視圖,其中 index 是一個 int。然后,您可以使用系結到 selection 屬性的 State 變數來了解選擇了哪個視圖。
import SwiftUI
struct SwiftUIView: View {
@State var tabSelected = 0
@State var weatherText = "is..."
var body: some View {
VStack {
Spacer()
Text("Weather: \(weatherText)")
.font(.largeTitle)
.bold()
Spacer()
TabView (selection: $tabSelected) {
// Add each of the carousel views, e.g.images
// First view in carousel
Image(systemName:"sun.max")
.resizable()
.scaledToFit()
.tag(1)
// Second view in carousel
Image(systemName:"cloud.rain")
.resizable()
.scaledToFit()
.tag(2)
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
Spacer()
Button("GET WEATHER..") {
if(tabSelected == 1)
{
weatherText = "It's sunny"
}
else if(tabSelected == 2)
{
weatherText = "It's raining"
}
}
Spacer()
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/454089.html
上一篇:無法將型別“[[String]]”的值轉換為預期的引數型別“KotlinArray<KotlinArray<NSString>>”
