我有两个视图:一个图像和一个形状,填充图像.一旦我翻转了图像应该显示的形状.以下是我的代码;

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()
            }
        }
    }
}

I am trying to have the image show up once the shape is tapped on and made to flip.

^当您点击形状时,它会翻转(不显示图像-错误)

推荐答案

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.因此,切换这一点.

Swift相关问答推荐

多个提取来计算核心数据中的记录

在用户将输入保存到SWIFT中的核心数据后,如何创建指向我的应用程序S主页的链接?

SwiftData:线程1:&Quot;NSFetchRequest找不到实体名称';提醒&q;的NSEntityDescription

了解SWIFT中的任务行为

如何在DATE()中进行比较

如何根据 DatePicker 中选定的日期实现 TabView 页面切换?

为什么Swift可以在没有足够信息的情况下推断类型?

如何避免从模块导入函数

在主要参与者中启动分离任务和调用非隔离函数之间的区别

使 Picker 与其他 BinaryInteger 类型兼容

引用类型(类)不需要但 struct 需要的可识别 id

Swift - 如何更新多目录中的对象

swift:修改字典中的数组

Alamofire 会自动存储 cookie 吗?

swift中的SequenceType和CollectionType有什么区别?

如何使用 Swift/iOS 为人类书写的笔画制作动画?

Swift:在子类中覆盖 == 导致仅在超类中调用 ==

快速在 LLDB 中使用 po

使用 phimagemanager 将图像保存到自定义相册?

iOS:如何检测用户是否订阅了自动更新订阅