Day 95 of #100DaysOfSwiftUI

Start 100DaysOfSwiftUI from 2020.Mar.18th.

Day 95: Milestone: Project 16-18
done with 2 hours

# still haptic does not work on my iPhone

コード

struct ContentView: View {
    @State private var diceRollresult = 1
    var body: some View {
        TabView {
            DiceRollView(rollResult: $diceRollresult)
                .tabItem{
                    Image(systemName: "number.square")
                    Text("Roll")
            }
            Text("ResultView")
                .tabItem {
                    Image(systemName: "list.number")
                    Text("Result")
            }
        }
    }
}
コード

struct DiceRollView: View {
    @Binding var rollResult: Int
    @State private var rollResults:[Int] = [Int](repeating: 1, count: 20)
    @State private var maxNum = 6
    @State private var diceNum = 1
    
    let haptic = UINotificationFeedbackGenerator()

    
    var body: some View {
        GeometryReader { geom in
            VStack {
                Text("how many dice?")
                Picker(selection: self.$diceNum, label: Text("How many dice"), content: {
                    Text("1").tag(1)
                    Text("2").tag(2)
                    Text("3").tag(3)
                })
                    .pickerStyle(SegmentedPickerStyle())
                Text("dice type")
                Picker(selection: self.$maxNum, label: Text("dice"), content: {
                    Text("4").tag(4)
                    Text("6").tag(6)
                    Text("8").tag(8)
                    Text("10").tag(10)
                    Text("12").tag(12)
                    Text("20").tag(20)
                })
                    .pickerStyle(SegmentedPickerStyle())
                HStack {
                    ForEach(0 ..< self.diceNum, id:\.self ) { index in
                        Image(systemName: self.imageName(num: self.rollResults[index]))
                        .resizable()
                        .scaledToFit()
                            .frame(width: geom.size.width / CGFloat(self.diceNum))
                        
                    }
                }
                Button(action: {
                    self.rollResult = 0
                    self.haptic.notificationOccurred(.success)
                    for index in 0 ..< self.diceNum {
                        self.rollResults[index] = Int.random(in: 1...self.maxNum)
                        self.rollResult += self.rollResults[index]
                    }
                }, label: {
                    Text("Tap dice to roll !")
                })
                Text("Total : \(self.rollResult)")
            }
        }
    }
    func imageName(num:Int) -> String {
        return String(format: "%02d.square", num)
    }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です