[英]How to get apple watch crown rotation direction when using SwiftUI?
I am building a watch app using SwiftUI. For some reason, I need to check if a user rotates the watch crown up or down then show or hide some UIs.我正在使用 SwiftUI 构建手表应用程序。出于某种原因,我需要检查用户是否向上或向下旋转表冠然后显示或隐藏一些 UI。 I only find this function
digitalCrownRotation
which can read values from the watch crown, but I have not figured out how to get the rotation directions.我只找到这个 function
digitalCrownRotation
可以从表冠读取值,但我还没有弄清楚如何获得旋转方向。
Any hint will be appreciated.任何提示将不胜感激。
Updata Sample code I am using digitalCrownRotation, but the problem is the scrollview does not scroll now.更新数据示例代码我正在使用digitalCrownRotation ,但问题是滚动视图现在不滚动。
struct ContentView: View {
@State private var up = false
@State private var scrollAmount = 0.0
@State private var prevScrollAmount = 0.0
var body: some View {
ScrollView {
Text("""
Here you can find activities to practise your reading skills. Reading will help you to improve your understanding of the language and build your vocabulary.
The self-study lessons in this section are written and organised according to the levels of the Common European Framework of Reference for languages (CEFR). There are different types of texts and interactive exercises that practise the reading skills you need to do well in your studies, to get ahead at work and to communicate in English in your free time.
"""
)
}
.focusable(true)
.digitalCrownRotation($scrollAmount)
.onChange(of: scrollAmount) { value in
self.up = (value > prevScrollAmount)
self.prevScrollAmount = value
if up {
print("up")
} else {
print("down")
}
}
}
}
To find the scroll direction, you will need to keep track of the previous scroll amount and check it with the current amount when the scrollAmount
changes.要找到滚动方向,您需要跟踪之前的滚动量,并在
scrollAmount
更改时将其与当前滚动量进行核对。 Here is a small complete example:这是一个完整的小例子:
struct ContentView: View {
@State private var up = false
@State private var scrollAmount = 0.0
@State private var prevScrollAmount = 0.0
var body: some View {
Text(up ? "Up" : "Down")
.focusable(true)
.digitalCrownRotation($scrollAmount)
.onChange(of: scrollAmount) { value in
self.up = (value > prevScrollAmount)
self.prevScrollAmount = value
}
}
}
For more information, read this article: How to read the Digital Crown on watchOS using digitalCrownRotation() .有关详细信息,请阅读本文: 如何使用 digitalCrownRotation() 在 watchOS 上读取数字表冠。
In a scrollview在滚动视图中
The crown can only control one view at a time, which I believe means you can't both directly control the scrollView and read the value with .digitalCrownRotation
.皇冠一次只能控制一个视图,我相信这意味着您不能既直接控制 scrollView 又使用
.digitalCrownRotation
读取值。 A way to workaround this limitation is to read the value with .digitalCrownRotation
and then set the .content.offset
of the scrollView directly.解决此限制的一种方法是使用
.digitalCrownRotation
读取值,然后直接设置 scrollView 的.content.offset
。
The problem with this solution is that it contains several magic numbers for the frame height and the scroll minimum and maximum values.这个解决方案的问题在于它包含几个用于帧高度和滚动最小值和最大值的幻数。 These were chosen to make all of the text appear.
选择这些是为了使所有文本都出现。 I know this isn't a perfect solution, but I put it here hoping it will give you something to build upon.
我知道这不是一个完美的解决方案,但我把它放在这里希望它能给你一些东西。
In this example, the background color is green when the scrollview is scrolling up and red when it is scrolling down.在此示例中,滚动视图向上滚动时背景颜色为绿色,向下滚动时背景颜色为红色。
struct ContentView: View {
@State private var up = false
@State private var scrollAmount = -190.0
@State private var prevScrollAmount = -190.0
var body: some View {
ScrollView {
Text("""
Here you can find activities to practise your reading skills. Reading will help you to improve your understanding of the language and build your vocabulary.
The self-study lessons in this section are written and organised according to the levels of the Common European Framework of Reference for languages (CEFR). There are different types of texts and interactive exercises that practise the reading skills you need to do well in your studies, to get ahead at work and to communicate in English in your free time.
"""
)
.frame(height: 530)
.focusable(true)
.digitalCrownRotation($scrollAmount, from: -190, through: 190)
.onChange(of: scrollAmount) { value in
self.up = (value > prevScrollAmount)
self.prevScrollAmount = value
if up {
print("up")
} else {
print("down")
}
}
}
.content.offset(x: 0, y: -CGFloat(scrollAmount))
.background(up ? Color.green : .red)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.