【发布时间】:2020-11-23 23:10:02
【问题描述】:
如何在移动元素移动的同时获得它的 Y 偏移量?
这是我正在运行的代码:
import SwiftUI
struct testView: View {
@State var showPopup: Bool = false
var body: some View {
ZStack {
VStack {
Button(action: {
self.showPopup.toggle()
}) {
Text("show popup")
}
Color.black
.frame(width: 200, height: 200)
.offset(y: showPopup ? 0 : UIScreen.main.bounds.height)
.animation(.easeInOut)
}
}
}
}
struct testView_Previews: PreviewProvider {
static var previews: some View {
testView()
}
}
我想在单击按钮时获取黑色正方形的 Y 值,该正方形将移动到 0 位置但是我想检测正方形何时达到 0 值我该怎么做?
【问题讨论】:
-
你需要你的 AnimatableModifier,然后你可以在 animatableData 中截取改变的值。比如stackoverflow.com/a/61017784/12299030。