这里的问题 在学习SwiftUI的过程中,这个应用程序专门针对WatchOS。所以我正在创建一个行视图,然后使用轮播视图。我有它,所以如果你点击列表项,它会要求你输入你的分数。当然,这进入了一个字符串(我希望我能让它变成一个整数,但对我不起作用。帧分数显示正常。但是,我正在尝试找出一种方法来正确添加总分。
例如
第 1 帧得分 5 总分 5
第 2 帧得分 2 总分 7
第 3 帧得分 10 总分 17
。
任何帮助将不胜感激,谢谢
struct StartBowlingView: View {
var body: some View {
List{
RowView(title: "1", framescore: "0", totalscore: "0")
RowView(title: "2", framescore: "0", totalscore: "0")
RowView(title: "3", framescore: "0", totalscore: "0")
RowView(title: "4", framescore: "0", totalscore: "0")
RowView(title: "5", framescore: "0", totalscore: "0")
RowView(title: "6", framescore: "0", totalscore: "0")
RowView(title: "7", framescore: "0", totalscore: "0")
RowView(title: "8", framescore: "0", totalscore: "0")
RowView(title: "9", framescore: "0", totalscore: "0")
RowView(title: "10", framescore: "0", totalscore: "0")
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.navigationBarTitle("Frame")
.listStyle(CarouselListStyle())
}
}
struct RowView: View {
@State var title: String
@State var framescore: String
@State var totalscore: String
var TotalScore: Int {
let Totalframescore = Int(framescore) ?? 0
return Totalframescore
}
var body: some View {
NavigationLink(destination: TextField("Enter your Frame Score", text: $framescore) .border(Color.black))
{ //Start Frame List Design View
VStack(alignment: .leading) {
HStack(alignment: .center) {
Text(title)
.font(.system(.headline, design: .rounded))
.foregroundColor(Color.blue)
.multilineTextAlignment(.leading)
Divider()
Spacer()
Text("(framescore)")
.font(.system(.body, design: .rounded))
.multilineTextAlignment(.leading)
Divider()
Spacer()
Text("(TotalScore)")
.font(.system(.headline, design: .rounded))
.foregroundColor(Color.green)
.multilineTextAlignment(.trailing)
}
}
.listRowBackground(Color.blue)
.frame(height: 60, alignment: .topTrailing)
}
}
}
一种方法可以在每一行上减少分数集。通过遍历一组分数,它可以计算帧数、帧分数和先前分数的总和。例如,您的主视图可能如下所示:
struct StartBowlingView: View {
@State var frameScores = [5, 2, 10]
var body: some View {
List{
ForEach(0..<self.frameScores.endIndex) { index in
RowView(title: "(index + 1)",
framescore: "(self.frameScores[index])",
totalscore: "(self.frameScores[0...index].reduce(0, { $0 + $1 }))")
}
}
}
}
我意识到保龄球分数可能比简单地对帧求和更复杂(尽管我不知道确切的逻辑(,但另一种方法是创建一个帧模型来跟踪多个 Int:
struct FrameScore {
let frameNumber: Int
let firstScore: Int
let secondScore: Int
let previousScore: Int
var frameScore: Int { return self.firstScore + self.secondScore }
var totalScore: Int { return self.frameScore + self.previousScore }
var isStrike: Bool { return self.firstScore == 10 }
var isSpare: Bool { return !self.isStrike && self.frameScore == 10 }
}
然后主视图可以更新以保留帧列表:
struct StartBowlingView: View {
@State var frames = [FrameScore]()
var body: some View {
List{
ForEach(self.frames, id: .frameNumber) { frame in
RowView(title: "(frame.frameNumber)",
framescore: "(frame.frameScore)",
totalscore: "(frame.totalScore)")
}
Button("Add Score") {
let first = Int.random(in: 0...10)
let second = Int.random(in: 0...(10 - first))
// Here's where to add some logic about the prevous frames' strikes and spares affecting the new frame/total score
self.frames.append(
FrameScore(frameNumber: self.frames.count + 1,
firstScore: first,
secondScore: second,
previousScore: self.frames.last?.totalScore ?? 0))
}
}
}
}