如何在 WatchOS 6 的 SwiftUI 中获取涂鸦输入
How to get scribble input in SwiftUI for WatchOS6
我正在尝试做到这一点,以便当用户单击其中包含评论的按钮时,它会弹出一个潦草的用户输入和听写。我查看了其他一些解决方案,但我无法让它们中的任何一个工作。我想这可能是因为它们都是在 watchOS3 中实现的建议。我还查看了 apple 文档并找到了这个 https://developer.apple.com/documentation/watchkit/wkinterfacecontroller/1619527-presenttextinputcontroller,但我无法让它工作。我的完整代码如下
import SwiftUI
import Foundation
struct ContentView: View {
@State var counterValues = [0,0,0, -1, -1, -1, -1, -1, -1, -1 ]
@State var counterNames = ["Pullups", "Pushups", "Situps", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
@State var counters = 3;
var body: some View {
ScrollView {
VStack {
ForEach( counterValues.indices ) { index in
if (self.counterValues[index]>=0) {
Text("\(self.counterNames[index])")
Button(action: {
self.counterValues[index] += 1
}) {
Text("\(self.counterValues[index])")
.font(.title)
.multilineTextAlignment(.center)
}
}
}
Button(action: {
//Add user input here
}) {
Image(systemName: "plus")
.font(.largeTitle)
.foregroundColor(.green)
}
.cornerRadius(40)
.shadow(color: .gray, radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/ )
.padding(20)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
这是一个特定的按钮,当我点击它时,我想让它允许用户输入,然后将该输入存储在一个变量中
Button(action: {
//Add user input here
}) {
Image(systemName: "plus")
.font(.largeTitle)
.foregroundColor(.green)
}
.cornerRadius(40)
.shadow(color: .gray, radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/ )
.padding(20)
您可以尝试使用以下带有文本字段的代码。
struct ContentView: View {
@State
var counterValues = [0,0,0, -1, -1, -1, -1, -1, -1, -1 ]
@State var counterNames = ["Pullups", "Pushups", "Situps", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
@State var counters = 3;
@State var textInput = "";
@State var showInput = false;
var body: some View {
ScrollView {
VStack {
ForEach( counterValues.indices ) { index in
if (self.counterValues[index]>=0) {
Text("\(self.counterNames[index])")
Button(action: {
self.counterValues[index] += 1
}) {
Text("\(self.counterValues[index])")
.font(.title)
.multilineTextAlignment(.center)
}
}
}
Button(action: {
//Add user input here
self.showInput.toggle()
}) {
Image(systemName: "plus")
.font(.largeTitle)
.foregroundColor(.green)
}
.cornerRadius(40)
.shadow(color: .gray, radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/ )
.padding(20)
if self.showInput{
TextField.init("input", text: $textInput)}
}
}
}
}
我正在尝试做到这一点,以便当用户单击其中包含评论的按钮时,它会弹出一个潦草的用户输入和听写。我查看了其他一些解决方案,但我无法让它们中的任何一个工作。我想这可能是因为它们都是在 watchOS3 中实现的建议。我还查看了 apple 文档并找到了这个 https://developer.apple.com/documentation/watchkit/wkinterfacecontroller/1619527-presenttextinputcontroller,但我无法让它工作。我的完整代码如下
import SwiftUI
import Foundation
struct ContentView: View {
@State var counterValues = [0,0,0, -1, -1, -1, -1, -1, -1, -1 ]
@State var counterNames = ["Pullups", "Pushups", "Situps", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
@State var counters = 3;
var body: some View {
ScrollView {
VStack {
ForEach( counterValues.indices ) { index in
if (self.counterValues[index]>=0) {
Text("\(self.counterNames[index])")
Button(action: {
self.counterValues[index] += 1
}) {
Text("\(self.counterValues[index])")
.font(.title)
.multilineTextAlignment(.center)
}
}
}
Button(action: {
//Add user input here
}) {
Image(systemName: "plus")
.font(.largeTitle)
.foregroundColor(.green)
}
.cornerRadius(40)
.shadow(color: .gray, radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/ )
.padding(20)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
这是一个特定的按钮,当我点击它时,我想让它允许用户输入,然后将该输入存储在一个变量中
Button(action: {
//Add user input here
}) {
Image(systemName: "plus")
.font(.largeTitle)
.foregroundColor(.green)
}
.cornerRadius(40)
.shadow(color: .gray, radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/ )
.padding(20)
您可以尝试使用以下带有文本字段的代码。
struct ContentView: View {
@State
var counterValues = [0,0,0, -1, -1, -1, -1, -1, -1, -1 ]
@State var counterNames = ["Pullups", "Pushups", "Situps", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
@State var counters = 3;
@State var textInput = "";
@State var showInput = false;
var body: some View {
ScrollView {
VStack {
ForEach( counterValues.indices ) { index in
if (self.counterValues[index]>=0) {
Text("\(self.counterNames[index])")
Button(action: {
self.counterValues[index] += 1
}) {
Text("\(self.counterValues[index])")
.font(.title)
.multilineTextAlignment(.center)
}
}
}
Button(action: {
//Add user input here
self.showInput.toggle()
}) {
Image(systemName: "plus")
.font(.largeTitle)
.foregroundColor(.green)
}
.cornerRadius(40)
.shadow(color: .gray, radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/ )
.padding(20)
if self.showInput{
TextField.init("input", text: $textInput)}
}
}
}
}