【发布时间】:2021-08-11 22:17:31
【问题描述】:
我希望在滚动时显示 Text,但在不滚动时使用 digitalCrownRotation 隐藏文本(如滚动时显示的指示器)。目前,当我滚动时它只能以一种方式工作并且不能很好地工作,这可以实现吗?
extension View {
func hidden(_ shouldHide: Bool) -> some View {
opacity(shouldHide ? 0 : 1)
}
}
struct ContentView: View {
@State var date: Date = Date()
@State var scroll: Double = 0.0
@State var previous: Double = 0.0
@State var scrolling: Bool = false
var body: some View {
VStack {
Text("\(date.dateFormat("E, d MMM"))")
.focusable(true)
.hidden(!scrolling)
.digitalCrownRotation($scroll, from: 0, through: 365, by: 1, sensitivity: .low, isContinuous: false, isHapticFeedbackEnabled: true)
.onChange(of: scroll) { value in
scrolling = (value > previous)
previous = value
date = Calendar.current.date(byAdding: .day, value: Int(value), to: Date())!
}
}
.onAppear {
self.date = Date()
}
}
}
【问题讨论】:
-
scrolling = value != previous -
@TamásSengel 对我不起作用,它必须是双向的,所以对于
digitalCrownRotation