【问题标题】:How to get apple watch crown rotation direction when using SwiftUI?使用 SwiftUI 时如何获取 Apple Watch 表冠旋转方向?
【发布时间】:2021-02-26 09:05:39
【问题描述】:

我正在使用 SwiftUI 构建一个手表应用程序。出于某种原因,我需要检查用户是否向上或向下旋转表冠,然后显示或隐藏一些 UI。 我只找到了digitalCrownRotation这个函数,它可以从表冠读取值,但我还没有弄清楚如何获取旋转方向。

任何提示将不胜感激。

更新数据 示例代码 我正在使用 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")
                    }
                   }
    }
}

【问题讨论】:

    标签: swift swiftui apple-watch watch watchos


    【解决方案1】:

    要找到滚动方向,您需要跟踪之前的滚动量,并在scrollAmount 更改时与当前量进行检查。这是一个完整的小例子:

    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
                }
        }
    }
    

    欲了解更多信息,请阅读这篇文章:How to read the Digital Crown on watchOS using digitalCrownRotation()


    在滚动视图中

    皇冠一次只能控制一个视图,我相信这意味着你不能既直接控制滚动视图又用.digitalCrownRotation读取值。解决此限制的一种方法是使用.digitalCrownRotation 读取值,然后直接设置滚动视图的.content.offset

    这个解决方案的问题在于它包含几个幻数,用于框架高度以及滚动最小值和最大值。选择这些是为了使所有文本都出现。我知道这不是一个完美的解决方案,但我把它放在这里希望它能给你一些基础。

    在本例中,滚动视图向上滚动时背景颜色为绿色,向下滚动时背景颜色为红色。

    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)
            
        }
    }
    

    【讨论】:

    • 感谢您的回答!我试过这种方式,它适用于Text()。但是当我将它用于 ScrollView 时,ScrollView 不会滚动,直到我先触摸它。
    • 请为您的问题添加一个最小可重现示例的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 2020-07-19
    • 1970-01-01
    相关资源
    最近更新 更多