簡體   English   中英

無法使觸覺反饋與 Swift 配合使用

[英]Cannot get Haptic Feedback to work with Swift

我是一名大學計算機科學專業的學生,他有一個 Apple Watch 應用程序的想法,該應用程序在每次達到特定時間間隔時都會振動,例如,用戶通過選擇器選擇 20 秒的間隔,點擊“開始”,應用程序將啟動計時器從 0 到 20,一旦達到 20 ,我希望手表振動並再次開始計數到 20,直到用戶選擇停止它。 我想用它來讓跑步者了解一致的跑步速度。 問題是我無法讓 UIImpactFeedbackGenerator 工作,也沒有其他觸覺反饋方法。 我沒有 swift 的經驗,只有 python 和 Java 但除了這個觸覺部分我基本上完成了。 我遇到的一個錯誤是 UIImpactFeedbackGenerator 超出 scope,其中生成器在 SecondView 下聲明。 提前謝謝你:這是我的代碼:

import SwiftUI
    

struct ContentView: View {
    @State var timerScreenShown = false
    @State var timeVal = 10
    
    var body: some View {
        VStack{
            Text("Select \(timeVal)s intervals").padding()

            Picker(
                selection: $timeVal,
                label: Text("")){
                    ForEach(10...120, id: \.self) {
                        Text("\($0)")
                    }
                }
            
            NavigationLink(destination: SecondView(timerScreenShown: $timerScreenShown, timeVal: timeVal), isActive: $timerScreenShown, label: {Text("Go")})
            
        }
    }
}

struct SecondView: View{
    @Binding var timerScreenShown:Bool
    @State var timeVal = 10
    @State var startVal = 0
    
    var body: some View {
        VStack{
            if timeVal > 0 {
                Text("Timer")
                    .font(.system(size: 14))
                Text("\(startVal)")
                    .font(.system(size: 40))
                    .onAppear(){
                        Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
                            if self.timeVal > 0{
                                self.startVal += 1
                                if self.timeVal == self.startVal{
                                    
                                    let generator = UIImpactFeedbackGenerator(style: .medium)
                                    generator.impactOccurred()
                                    
                                    self.startVal = 0
                                }
                            }
                        }
                    }
                Text("seconds")
                    .font(.system(size: 14))
                Button(action: {
                    self.timerScreenShown = false
                }) {
                    Text("Cancel")
                        .foregroundColor(.red)
                }
            } else {
                Button(action: {
                    self.timerScreenShown = false
                }) {
                    Text("Done")
                        .foregroundColor(.green)
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
            ContentView()
        }
    }
}

WatchOs 確實有 play(_:) - 通知方法。

試試WKInterfaceDevice.current().play(.click)

有關更多信息,請查看文檔——https://developer.apple.com/documentation/watchkit/wkinterfacedevice/1628128-play

暫無
暫無

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

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