ios - SwiftUI .onTapGesture 从未调用过

标签 ios swift swiftui

附件是一个简单的 SwiftUI 结构,它显示了一组“扩展按钮”。我显示顶部按钮(其他 3 个是 1x1,在顶部按钮后面。)当用户点击顶部按钮时,它会扩展到 40x40 并以漂亮的动画移动下面的按钮。

我遇到的问题是没有任何底层按钮会响应它们的 .onTapGesture。我可以再次点击顶部按钮,动画反转,但是当点击其他按钮时没有任何反应。

import SwiftUI

struct MapTools: View {

    @State private var isRotated = false

    var body: some View {
        ZStack {
            Image("wheel")
                .resizable()
                .frame(width: 1, height: 1)
                .foregroundColor(.mFoodColor)
                .scaleEffect(self.isRotated ? 40 : 1)
                .offset(x: self.isRotated ? -60 : 0, y: 0)
                .onTapGesture {
                    withAnimation {
                        self.isRotated.toggle()
                    }
                }

            Image("locationPin")
                .resizable()
                .frame(width: 1, height: 1)
                .foregroundColor(.mFoodColor)
                .scaleEffect(self.isRotated ? 40 : 1)
                .offset(x: self.isRotated ? -60 : 0, y: self.isRotated ? 60 : 0)
                .onTapGesture {
                    withAnimation {
                        self.isRotated.toggle()
                    }
                }

            Image("locationArrow")
                .resizable()
                .frame(width: 1, height: 1)
                .foregroundColor(.mFoodColor)
                .scaleEffect(self.isRotated ? 40 : 1)
                .offset(x: 0, y: self.isRotated ? 60 : 0)
                .onTapGesture {
                    withAnimation {
                        self.isRotated.toggle()
                    }
                }

            Image("mapTools")
                .resizable()
                .frame(width: 40, height: 40)
                .foregroundColor(.mFoodColor)
                .rotationEffect(self.isRotated ? .degrees(90) : .degrees(0))
                .onTapGesture {
                    withAnimation {
                        self.isRotated.toggle()
                    }
                }
            }
        }
}

有人看到我做错了什么吗?

最佳答案

You have to use different bools for different Images to rotate them. Try with declaring 4 booleans for your four different images to rotate them

@State private var isFirstRotated = false
@State private var isSecondRotated = false
@State private var isThirdRotated = false
@State private var isFourthRotated = false

关于ios - SwiftUI .onTapGesture 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61974714/

相关文章:

ios - UISplitView 横向模式

ios - cellForRowAtIndexPath 和 prepareForSegue 返回不同的标签颜色

ios - 自动 Cloud Firestore 更新

ios - 设置默认缓存策略 apollo swift

ios - SwiftUI 表单无法使用 Spacer() 正确定位

ios - 如何在 iOS 应用程序上接收连接的 WiFi 模块/路由器发送的数据?

ios - CoreData严重应用错误: Entity is not key value coding-compliant

ios - 无法将不可变值作为 inout 参数 : literals are not mutable, 传递,为什么?

ios - 我应该如何触发 SwiftUI 应用程序中的网络调用以刷新应用程序打开时的数据?

uikit - 使自定义 SwiftUI View 适应内置修饰符