SwiftUI获取当前旋转值
我想从roation3DEffect中获取当前旋转值(以度为单位) 如果值通过-90,我想更改文本 我如何获得该值 这是我目前的代码:SwiftUI获取当前旋转值,swiftui,Swiftui,我想从roation3DEffect中获取当前旋转值(以度为单位) 如果值通过-90,我想更改文本 我如何获得该值 这是我目前的代码: @State var rotation = false […] ZStack { Rectangle() .foregroundColor(.green) Text("1") } .frame(width: 120, height: 60) .rotation3DEffect(Angle(degrees: rotation
@State var rotation = false
[…]
ZStack {
Rectangle()
.foregroundColor(.green)
Text("1")
}
.frame(width: 120, height: 60)
.rotation3DEffect(Angle(degrees: rotation ? -180 : 0), axis: (x: 10, y: 0, z: 0), anchor: .bottom)
.animation(.linear(duration: 0.25))
.onTapGesture {
self.rotation.toggle()
}
您可以这样做:
struct ContentView: View {
@State private var degree = 0.0
@State private var text = "1"
var body: some View {
ZStack {
Rectangle()
.foregroundColor(.green)
Text(text)
}
.frame(width: 120, height: 60)
.rotation3DEffect(Angle(degrees: degree), axis: (x: 10, y: 0, z: 0), anchor: .bottom)
.onTapGesture {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.text = ""
}
withAnimation(.linear(duration: 1)) {
if self.degree == 0 {
self.degree += 180
} else {
self.degree -= 180
}
}
}
}
}
根据您的评论编辑了我的代码。谢谢您的回复。我试图在透视图中隐藏文本更改。因此,变化应发生在90°。您对这种特殊行为有什么建议吗?请参阅我的更新代码。诀窍是找到正确的时间(整个动画时间的50%),而不是特定的90°值。