簡體   English   中英

使用SwiftUI時如何獲取Apple Watch表冠旋轉方向?

[英]How to get apple watch crown rotation direction when using SwiftUI?

我正在使用 SwiftUI 構建手表應用程序。出於某種原因,我需要檢查用戶是否向上或向下旋轉表冠然后顯示或隱藏一些 UI。 我只找到這個 function 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")
                    }
                   }
    }
}

要找到滾動方向,您需要跟蹤之前的滾動量,並在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
            }
    }
}

有關詳細信息,請閱讀本文: 如何使用 digitalCrownRotation() 在 watchOS 上讀取數字表冠


在滾動視圖中

皇冠一次只能控制一個視圖,我相信這意味着您不能既直接控制 scrollView 又使用.digitalCrownRotation讀取值。 解決此限制的一種方法是使用.digitalCrownRotation讀取值,然后直接設置 scrollView 的.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)
        
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM