為高級專案創建模擬應用程式。我創建了一個主頁,然后呼叫其他視圖在堆疊中顯示它們。底部視圖包括影像內的導航鏈接,單擊時應將用戶發送到新頁面。圖片在點擊時沒有回應。我試圖將它們嵌入到 NavigationView 中,但隨后完全搞砸了主頁。我是 Swift 新手,無法在其他任何地方找到此問題,也無法對其進行故障排除。任何幫助將不勝感激,謝謝!
主頁圖片
小卡片視圖的影像
import SwiftUI
struct ViewA: View {
var body: some View {
NavigationView {
ZStack {
HStack {
smallCardsView()
.offset(y:60)
}
Color(#colorLiteral(red: 0.1927102208, green: 0.282566458, blue: 0.3712197244, alpha: 1))
.frame(width: 500, height: 1000, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
Image("dumbell")
.resizable()
.frame(width: 380, height: 230, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
.offset(y:-8)
.opacity(0.2)
Divider().frame(width: 350, height: 10, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/).background(Color(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)))
.cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
.padding(.bottom,30)
Text("Weekly Workouts")
.font(.largeTitle)
.fontWeight(.medium)
.padding(.top, 240)
.foregroundColor(Color(#colorLiteral(red: 0.9327766299, green: 0.3332475424, blue: 0.3345346749, alpha: 1)))
VStack {
Spacer()
.frame(height:200)
LaidView()
HStack {
smallCardsView()
.offset(y:60)
}
.frame(height:280)
.cornerRadius(10)
}
}
}
}
}
struct ViewA_Previews: PreviewProvider {
static var previews: some View {
ViewA()
}
}
主頁底視圖代碼(SmallCardsView)
import SwiftUI
struct smallCardsView : View {
var body: some View {
// ZStack {
HStack{
ZStack {
NavigationLink(destination: Push()){
Image("bench-1")
.resizable()
.cornerRadius(12.0)
.shadow(radius:50 )
.frame(width: 125, height: 150)
.blur(radius: 2.0)
}
Rectangle()
.fill(Color(.black))
.opacity(0.2)
.frame(width: 125, height: 150, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
.cornerRadius(12.0)
Text("Push")
.fontWeight(/*@START_MENU_TOKEN@*/.bold/*@END_MENU_TOKEN@*/)
.font(.largeTitle)
}
ZStack {
NavigationLink(destination: Pullworkouts()){
Image("bentover-1")
.resizable()
.cornerRadius(12.0)
.shadow(radius:50 )
.frame(width:125, height: 150)
.blur(radius: 2.0)
}
Rectangle()
.fill(Color(.black))
.opacity(0.2)
.frame(width: 120, height: 150, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
.cornerRadius(12.0)
Text("Pull")
.fontWeight(/*@START_MENU_TOKEN@*/.bold/*@END_MENU_TOKEN@*/)
.font(.largeTitle)
}
ZStack {
NavigationLink(destination: Legs()){
Image("backsquat")
.resizable()
.cornerRadius(12.0)
.shadow(radius:50 )
.frame(width: 120, height: 150)
.blur(radius: 2.0)
}
Rectangle()
.fill(Color(.black))
.opacity(0.3)
.frame(width: 120, height: 150, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
.cornerRadius(12.0)
Text("Legs")
.fontWeight(/*@START_MENU_TOKEN@*/.bold/*@END_MENU_TOKEN@*/)
.font(.largeTitle)
}
}
// }
.foregroundColor(.white)
}
}
struct smallCards_Previews: PreviewProvider {
static var previews: some View {
smallCardsView()
}
}
uj5u.com熱心網友回復:
在 MVVM 模式中解決您的案例,我嘗試簡化示例以便于理解概念。此示例有助于避免重復您的代碼并使其更加靈活和可支持
首先,您需要為您應用的每個鍛煉創建實體。您可以在此處添加任何資料以在視圖中顯示
struct Workout: Identifiable {
let id = UUID().uuidString
let name: String
let image: String
}
其次需要創建包含所有鍛煉的 ViewModel
class WorkoutViewModel: ObservableObject {
@Published var workouts: [Workout] = [
Workout(name: "Push", image: "flame.fill"),
Workout(name: "Pull", image: "externaldrive.connected.to.line.below"),
Workout(name: "Legs", image: "florinsign.circle")
]
}
創建資料后,您可以跳轉到掌握 CardView 和 DetailView
struct CardView: View {
var workout: Workout
var body: some View {
VStack {
Image(systemName: workout.image)
.resizable()
.frame(width: 75, height: 75)
Text(workout.name)
.font(.subheadline)
}
}
}
struct CardDetailView: View {
var workout: Workout
var body: some View {
VStack {
Image(systemName: workout.image)
.resizable()
.frame(width: 150, height: 150)
Text(workout.name)
.font(.title)
.fontWeight(.semibold)
}
}
}
最后將它們包裝在一起
struct FinalView: View {
@StateObject var vm = WorkoutViewModel()
var body: some View {
NavigationView {
HStack(spacing: 20) {
ForEach(vm.workouts) { workout in
NavigationLink(destination: CardDetailView(workout: workout)) {
CardView(workout: workout)
}
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/459927.html
