SwiftUI-Day17 Project1-2

文章目录

  • 吐槽
  • 快捷键
  • 代码
  • 效果

吐槽

Day13-15是复习,看了下,有些小技巧,原文链接在这里

这个project是做一个AA付款的程序,用来计算每个人要给多少钱。直接跳过一堆介绍,开始代码。
本文参考资料:
https://www.hackingwithswift.com/100/swiftui/17
时间:17 October, 2020

快捷键

运行快捷键:command+R
刷新:option+command+P
隐藏/显示preview:option+command+回车
将preview转为代码(恢复用上面的显示preview):control+option+command+回车
删除当前行:option+D
点击输入框显示键盘:shift+command+K

代码

import SwiftUI

struct ThirdPage: View {
    @State private var checkAmount = ""
    @State private var numberOfPeople = 2
    @State private var tipIndex = 2
    @State private var tipPercentages = [10, 15, 20, 25,0]
    
    var totalPerPerson: Double{
        let peopleCount = Double(numberOfPeople + 2)
        let tipSelection = Double(tipPercentages[tipIndex])
        let orderAmount = Double(checkAmount) ?? 0
        
        let tipValue = orderAmount / 100 * tipSelection
        let totalAmount  = tipValue + orderAmount
        let amountPerPerson = totalAmount / peopleCount
        
        return amountPerPerson
    }
    
    var body: some View {
    // 不加navigationview点不开链接
        NavigationView{
            Form{
                Section(header: Text("Please Enter Your Amount").textCase(.none)){
                    TextField("Amount", text: $checkAmount)
						// keyboardtype
                        .keyboardType(.decimalPad)
                    Picker("Number of People", selection: $numberOfPeople){
                        //numberofpeople is an index, so it shows 4 people instead of 2
                        ForEach(2 ..< 100){
                            Text("\($0) People")
                        }
                    }
                }
                Section(header: Text("How much tip do you want to leave?").textCase(.none)){
                    Picker("Tip Percentage", selection: $tipIndex){
                        ForEach(0 ..< tipPercentages.count) {
                            Text("\(tipPercentages[$0])%")
                        }
                    }
                    //Segment Picker Style - Segmented control
                    .pickerStyle(SegmentedPickerStyle())
                }
                Section(header: Text("hi")){
                    Text("$\(totalPerPerson, specifier: "%.2f")")
                }
            }
            .navigationTitle("Splite Form")// new features
        }
    }
}


struct ThirdPage_Previews: PreviewProvider {
    static var previews: some View {
        ThirdPage()
    }
}

效果

SwiftUI-Day17 Project1-2_第1张图片

你可能感兴趣的:(swift)