SwiftUI .onTapGesture从未调用

时间:2020-05-23 15:54:10

标签: 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()
                    }
                }
            }
        }
}

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

2 个答案:

答案 0 :(得分:0)

您必须对不同的图像使用不同的布尔值以旋转它们。 尝试为四个不同的图像声明4个布尔值以旋转它们

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

答案 1 :(得分:0)

发现这是来自另一个物体的干扰。