Hi,
I'm trying to convert an already Swift 5 game to SwiftUI. Almost everything works except when it's time to change the image based on the choice. Here's the code:
import SwiftUI
import Foundation
struct ContentView: View {
@State private var randomIndex: Int = 0
var body: some View {
VStack() {
Image("TwoSides")
.resizable()
.frame(width: 170, height: 90, alignment: .center)
.position(x: 100, y: 40)
Text("Heads or Tails").fontWeight(.bold)
.frame(width: CGFloat(200), height: CGFloat(5), alignment: .center)
.position(x: 90, y: 40)
Button(action: {
let array = ["Heads","Tails"]
let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
if (randomIndex == 1)
{
print("Heads")
Image("Heads")
.resizable()
.frame(width: 170, height: 90,alignment: .center)
}else {
print("Tails")
Image("Tails")
.resizable()
.frame(width: 170, height: 90,alignment: .center)
.position(x: 100, y: 40)
}
}) {
VStack {
Text("Flip Coin")
.padding(15)
.background(Color.green)
.foregroundColor(Color.white)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Any ideas would be greatful.
Dan Uff