在重置时通过手势状态控制动画的变化,可以使用@GestureState属性。
@GestureState是一个属性包装器,它提供了对拖放和其他手势操作的状态跟踪,并在手势结束时自动重置。
下面是一个示例,演示了如何在重置手势时使用@GestureState来控制动画变化:
struct ContentView: View {
@GestureState var draggingOffset: CGSize = .zero
var body: some View {
Circle()
.offset(draggingOffset)
.gesture(
DragGesture()
.updating($draggingOffset) { value, state, _ in
state = value.translation
}
.onEnded { _ in
withAnimation {
draggingOffset = .zero
}
}
)
}
}
在这个例子中,我们使用@GestureState来跟踪拖动的偏移量,并在手势结束时将其重置为零。
在“onEnded”闭包中,我们使用withAnimation来触发动画,以平滑地将偏移量返回到零。
@GestureState还可以与其他属性包装器一起使用,例如State和Animation,以创建复杂的交互效果。