Start 100DaysOfSwiftUI from 2020.Mar.18th.
Day 95: Milestone: Project 16-18
done with 2 hours
# still haptic does not work on my iPhone
コード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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") } } } } |
コード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
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) } } |
Sponsor Link