【发布时间】:2020-05-23 15:54:10
【问题描述】:
Attached 是一个简单的 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()
}
}
}
}
}
有人知道我做错了吗?
【问题讨论】:
-
可能不在提供的代码中,因为它只是在预览中按原样工作。 Xcode 11.4。好的,不完全是,因为我用系统 SF 替换了您的自定义图像。
-
我同意。您的代码看起来正确,可能是 Apple 错误。
-
@Asperi,首先感谢代码清理。其次,我对您的评论的解释是否正确,因为您单独尝试了它并且它有效?这是仅在预览模式下还是已编译?
-
无处不在