如何为两个偏移轴设置动画
我如何独立地为两个 .offset()
轴设置动画?这看起来像一个错误,对吗?蓝色应该不断上下弹跳,但一旦我尝试为 x
偏移设置动画,y
动画就会停止。
@State var offsetX = 0.0
@State var offsetY = 0.0
var body: some View {
VStack {
Color.blue
.frame(width: 50, height: 50)
.offset(x: offsetX, y: offsetY)
.onAppear {
withAnimation(.easeInOut.repeatForever(autoreverses: true)) {
offsetY = 300
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
withAnimation {
offsetX = 100
}
}
}
}
}
How can i animate both .offset()
axes independently? This looks like a bug right? The blue should keep bouncing up and down but the y
animation stops once I try to animate the x
offset.
@State var offsetX = 0.0
@State var offsetY = 0.0
var body: some View {
VStack {
Color.blue
.frame(width: 50, height: 50)
.offset(x: offsetX, y: offsetY)
.onAppear {
withAnimation(.easeInOut.repeatForever(autoreverses: true)) {
offsetY = 300
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
withAnimation {
offsetX = 100
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来有点矫枉过正,但我制作了一个自定义的 GeometryEffect,并且它独立地制作动画:
Sounds a bit overkill, but I made a custom GeometryEffect, and it animates independently: