如何在 SwiftUI 中随机化 Slots 应用程序的图片

How to randomize pictures for a Slots App in SwiftUI

我正在 SwiftUI 中制作一个非常简单的老虎机应用程序(出于学习语言的目的),我正在尝试制作它以便当用户单击“旋转”按钮时它会更改图片但不能似乎弄清楚该怎么做。我在想一些事情,比如将图片放在列表中,然后使用 .shuffle() 方法对它们进行洗牌,最后使用 for 循环挑选出每张图片并将其显示在屏幕上,但我无法完成任何原因。


import SwiftUI

struct ContentView: View {
    
    @State private var credits = 50
    @State private var Slot1 = "apple"
    @State private var Slot2 = "cherry"
    @State private var Slot3 = "star"
    @State var pictures = ["apple", "cherry", "star"]

    
    var body: some View {
        ZStack {
            VStack {
                Text("Slots using SwiftUI")
                    .font(.largeTitle)
                    .padding(.top, 50.0)
                Spacer()
                Text("Credits: "+String(credits))
                    .font(.title2)
                Spacer()
                
                HStack {
                    // Randomize the 3 pictures
                    
                    
                    
                    // "Print" the three pictures
                    Image(Slot1).resizable().aspectRatio(contentMode: .fit)
                    Image(Slot2).resizable().aspectRatio(contentMode: .fit)
                    Image(Slot3).resizable().aspectRatio(contentMode: .fit)
                }
                
                Spacer()
                
                Button(action: { }, label: {
                    ZStack {
                        RoundedRectangle(cornerRadius: 25)
                            .frame(width: 200, height: 50)
                        Text("Spin")
                            .font(.title2)
                            .fontWeight(.heavy)
                            .foregroundColor(Color.white)
                        
                        let shuffled = pictures.shuffled()
                        
                        
                    }
                    
                })
                
                Spacer()
            
                
                
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Layout/Design

如有任何帮助,我们将不胜感激。

只需添加如下内容:

func shuffle() {
   Slot1 = pictures[Int.random(in: 0..<3)]
   Slot2 = pictures[Int.random(in: 0..<3)]
   Slot3 = pictures[Int.random(in: 0..<3)]
}

并在按钮操作上调用它。

您应该在 Button 的操作中执行此操作,就像在代码中这样,在 pictures.indices 范围 中使用 random:


import SwiftUI

struct ContentView: View {
    
    @State private var credits = 50
    @State private var Slot1 = "apple"
    @State private var Slot2 = "cherry"
    @State private var Slot3 = "star"
    @State var pictures = ["apple", "cherry", "star"]
    
    
    var body: some View {
        ZStack {
            
            VStack {
                
                Text("Slots using SwiftUI")
                    .font(.largeTitle)
                    .padding(.top, 50.0)
                
                Spacer()
                
                Text("Credits: " + credits.description)
                    .font(.title2)
                
                Spacer()
                
                HStack {
                    
                    Image(Slot1).resizable().aspectRatio(contentMode: .fit)
                    Image(Slot2).resizable().aspectRatio(contentMode: .fit)
                    Image(Slot3).resizable().aspectRatio(contentMode: .fit)
                }
                
                Spacer()
                
                Button(action: {
                    
                    Slot1 = pictures[Int.random(in: pictures.indices)]    //<<: Here!
                    Slot2 = pictures[Int.random(in: pictures.indices)]    //<<: Here!
                    Slot3 = pictures[Int.random(in: pictures.indices)]    //<<: Here!
                    
                }, label: {
                    
                    ZStack {
                        RoundedRectangle(cornerRadius: 25)
                            .frame(width: 200, height: 50)
                        
                        Text("Spin")
                            .font(.title2)
                            .fontWeight(.heavy)
                            .foregroundColor(Color.white)
                    }
                    
                })
                
                Spacer()
                
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}