在学习SwiftUI的过程中,此应用专门用于WatchOS。因此,我创建了一个行视图,然后使用轮播视图。我有它,所以如果您单击列表项,它会要求您输入分数。当然,这会变成一个字符串(我希望我可以将它传递给一个int,但对我来说不起作用。)框架得分显示得很好。但是,我正在尝试找出一种方法来使总分正确添加。
例如,
赛局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))
}
}
}
}
想对此表示非常感谢!这很容易使我走上正轨。