我在SwiftUI中有一个表示股票assets资源 价格的图表.用户可以在呈现与拖动手势一起移动的覆盖线视图的图形上拖动.此覆盖视图的目的是确定用户正在分析的图形的特定点.
当用户拖动图形时,一切工作正常,视图出现,但覆盖线视图不稳定,不能顺畅地移动.唯一的动画是显示覆盖和隐藏它,这只在手势的开始和结束时完成,所以我不确定为什么我会得到这个错误的用户界面.
如果我将这条线拖来拖go 几秒钟,控制台中会出现以下警告:
无效的示例AnimatablePair<;AnimatablePair<;CGFloat,CGFloat>;,AnimatablePair<;CGFloat,CGFloat;>;(第一:SwiftUI.AnimatablePair<;CoreGraphics.CGFloat,核心图形.CGFloat>;(第一:11.0,第二:0.0),第二:SwiftUI.AnimatablePair<;CoreGraphics.CGFloat,核心图形.CGFloat>;(第一:0.0,第二:0.0),时间(秒:0.0)>;上次时间(秒:0.01664249994792044)`
代码:
GraphView(coin: data).frame(height: 300)
.gesture(DragGesture().onChanged({ value in
if abs(value.translation.width) > abs(value.translation.height) {
withAnimation { showPlot = true }
barTranslation = value.location.x
}
}).onEnded({ value in
withAnimation { showPlot = false }
}).updating($isDrag, body: { value, out, _ in
out = true
}))
.onChange(of: isDrag) { _ in
if !isDrag {
showPlot = false
}
}
.overlay(alignment: .bottomLeading) {
VStack(spacing: 0){
Text(currentPlot)
.font(.caption.bold())
.foregroundStyle(.gray)
.padding(.bottom, 35)
.position(x: barTranslation)
Rectangle()
.fill(.gray)
.frame(width: 1, height: 300)
.position(x: barTranslation)
Spacer()
}
.opacity(showPlot ? 1 : 0)
}