【发布时间】:2021-03-22 07:59:14
【问题描述】:
我很难弄清楚这个动画的事情。问题是,.onAppear 摇晃动画(应该永远循环)在视图被拖动后立即停止并冻结(它有另一个动画,以便它平滑地移动到拖动位置)。
有没有办法让 onAppear 动画独立于被拖动的视图运行?需要注意的重要一点是,我确实需要依赖于数组的位置(因此拖动手势直接写入数据),而我不需要摆动(大小动画)完全绑定到数组。
动画在拖动时冻结的插图(不受欢迎 - 想要在被拖动期间和之后继续摆动)
请看代码:
var body: some View {
Circle()
// .resizable()
.frame(width:size.width, height: size.height) //self.size is a state var
.foregroundColor(color)
.position(pos) //pos is a normal var bound to an array row
.gesture(DragGesture()
.onChanged { gesture in
withAnimation(.default) {
ballStorage.balls[numberInArray].position = gesture.location
}
}
.onEnded { gesture in
ballStorage.checkOverlaps(for: numberInArray)
}
) .onAppear {
withAnimation(Animation.easeInOut(duration: 1).repeatForever(autoreverses: true)) {
size = CGSize(width: size.width+20, height: size.height+20)
}
}
}
}
【问题讨论】:
-
您能否发布视频或完整代码,以便我们了解您在做什么或您想要什么。
-
也许只是重新添加动画 .onEnded 的 DragGesture() ?
标签: swift xcode animation swiftui