我有 2 個視圖:一個影像和一個填充影像的形狀。一旦我翻轉了影像應該顯示的形狀。這是我的代碼;
struct DogCard: View {
@State var animationAmount = 0.0
@State var isFaceUp: Bool = false
var card: MemoryGame.Card
var body: some View {
ZStack{
let shape = RoundedRectangle(cornerRadius: 10)
if card.isFaceUp {
shape.stroke(lineWidth: 3).foregroundColor(.blue)
Image(card.content).resizable().scaledToFit().padding()
}
else {
shape.fill().foregroundColor(.blue)
}
}
.rotation3DEffect(.degrees(animationAmount), axis: (x: 0, y: 1, z: 0))
.onTapGesture {
withAnimation {
animationAmount = 180
isFaceUp.toggle()
}
}
}
}
一旦點擊形狀并使其翻轉,我試圖讓影像顯示出來。
^ 當你點擊它時,形狀會翻轉(沒有顯示影像 - 錯誤)
uj5u.com熱心網友回復:
在withAnimation中,使用card.isFaceUp.toggle()代替isFaceUp.toggle()。嘗試這個:
@State var card: MemoryGame.Card // <-- here, @Binding if need be
// ....
.onTapGesture {
withAnimation {
animationAmount = 180
card.isFaceUp.toggle() // <-- here
}
}
您沒有使用代碼獲取影像的原因是因為您if card.isFaceUp {...}不是基于您的 var isFaceUp,而是基于card.isFaceUp. 所以切換那個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533043.html
標籤:迅速迅捷
上一篇:Swift:URLSession擴展配置錯誤:無法呼叫非函式型別“URLSession”的值
下一篇:SwiftUI視圖API模式
