Post

Replies

Boosts

Views

Activity

How to sum the same category total in SwiftUI?
Hi, guys I have a array like this: [ [icon: "A", amount: 11], [icon: "B", amount: 10], [icon: "A", amount: 15], [icon: "B", amount: 13], [icon: "C", amount: 5] ] Right now I only can show them in list one by one like: icon: "A", amount: 11 icon: "B", amount: 10 icon: "A", amount: 15 icon: "B", amount: 13 icon: "C", amount: 5 How can i show them in a List like: icon: "A", amount: 26 icon: "B", amount: 23 icon: "C", amount: 5 I appreciate it for any help and suggestions !
7
0
2.1k
Feb ’22
Cannot convert value of type 'ChooseModel.Angle' to expected argument type 'Angle'
Hi, guys i got errors from my View like below : Is anyone who can help me to fix it? My model like:     var id = UUID()     var decision: String     var choices: [String]     var startAngle: Angle     var endAngle: Angle           @CodableColor var color: UIColor     struct Angle: Codable, Equatable {         var degrees: Double     } } My model view like: import SwiftUI class ChooseModelView: ObservableObject {     var midRadians: Double {         return Double.pi / 2.0 - (model.startAngle.degrees + model.endAngle.degrees) * .pi / 180 / 2.0     }     @Published var model = ChooseModel(decision: "What to eat tonight?", choices: ["Sushi", "Ramen", "Korean BBQ", "Chinese"], startAngle: ChooseModel.Angle(degrees: 0.0), endAngle: ChooseModel.Angle(degrees: 90.0), color: UIColor.green) My view like :                     let width: CGFloat = min(geometry.size.width, geometry.size.height)                     let height = width                     let center = CGPoint(x: width * 0.5, y: height * 0.5)                     path.move(to: center)                     path.addArc(                         center: center,                         radius: width * 0.5,                         startAngle: Angle(degrees: -90.0) + chooseMV.model.startAngle,                         endAngle: Angle(degrees: -90.0) + chooseMV.model.endAngle,                         clockwise: false)                     path.closeSubpath()                 }
5
0
3.3k
Dec ’21
How can i save Angle and [Color] to UserDefaults ?
Hi everyone I'm new to SwiftUI and i try to make a simple app for random choices,So when you create a list choices that will turn to a Spin-WheelView.For now, I face two problems: First. I don't know how save Angle and Array to UserDefaults; Second: I don't know to connect choices view to spin-wheel view. Any helps i will appreciate it! thank you all. My model: import SwiftUI struct ChooseModel: Identifiable, Codable {     var id = UUID()     var decision: String     var choices: [String]     // i try to create Angle and [Colors] in my model,     // but they don't confirmed Codable, i don't know how to save them to User defaults. //    var startAngle: Angle //    var endAngle: Angle //    var colors: [Color] } Model View import SwiftUI class ChooseModelView: ObservableObject {     @Published var models = [ChooseModel]() {         didSet {             let encoder = JSONEncoder()             if let encoded = try? encoder.encode(models) {                 UserDefaults.standard.set(encoded, forKey: "models")             }         }     }     init() {         if let items = UserDefaults.standard.data(forKey: "models") {             let decoder = JSONDecoder()             if let decoded = try? decoder.decode([ChooseModel].self, from: items) {                 self.models = decoded                 return             }         }         self.models = []     } } My View: struct ChooseView: View {          @ObservedObject var chooseMV = ChooseModelView()     @State private var showingAddView = false          var body: some View {         NavigationView {             List {                 ForEach(chooseMV.models) { item in                     // Use NavigationLink to Each WheelView                     HStack {                         VStack(alignment: .leading) {                             Text(item.decision)                                 .font(.title3)                             HStack {                                 ForEach(item.choices, id: \.self) { choice in                                     Text("\(choice),")                                         .foregroundColor(Color.green)                                 }                             }                         }                         Spacer()                     }                                      }.onDelete(perform: remove)             }.navigationTitle("iChoose")                 .navigationBarItems(leading: EditButton(), trailing: Button(action: {                     self.showingAddView = true                 }, label: {                     Image(systemName: "plus")                 }))                 .sheet(isPresented: $showingAddView) {                     AddView(chooseMV: self.chooseMV)                 }         }     }     func remove(at offsets: IndexSet) {         chooseMV.models.remove(atOffsets: offsets)     } }
2
0
911
Dec ’21
How to get index in an array from an random rotation degrees?
Hi guys, I'm new to SwiftUI and I'm still working on my spinning wheel. For now I wanna show people the results on screen by Text("(Array[index])"). the question is i know the random rotation degrees but i don't know how to transfer it to that "result angles" index :P ,Any help I will appreciate it ! I use Path to draw the wheel: var slices: Array<OneChance.OneChanceData> {         let sum = Double(newChoices.count)         var endDeg: Double = 0.0         var tempSlices: [OneChance.OneChanceData] = []         for (color, text) in zip(colors, newChoices) {             let degrees: Double = Double(360 / sum)                          tempSlices.append(OneChance.OneChanceData(startAngle: Angle(degrees: endDeg), endAngle: Angle(degrees: endDeg + degrees), text: text, color: color))             endDeg += degrees         }         return tempSlices     } The spinning wheel view: ForEach(0..<chanceVM.newChoices.count) { i in                         EachSliceView(chanceData: self.chanceVM.slices[i])                     } I use onTapGesture to get random wheel spinning degrees: .onTapGesture {                             self.chanceVM.showResult = false                             self.chanceVM.rotateDegree = 0.0                             playSound(sound: "wheelSpinning13s", type: "mp3")                             self.chanceVM.allowToTapAgain = false                             withAnimation(Animation.easeOut(duration: 13.0)) {                                 self.chanceVM.rotateDegree = Double.random(in: 5400.0...7200.0)                             }                             print("\(chanceVM.rotateDegree)")                             print("\((Int(chanceVM.rotateDegree) % 360))")                             print("\((Int(chanceVM.rotateDegree) % 360)/(360/chanceVM.newChoices.count))")                             self.chanceVM.delayText()                             self.chanceVM.delayTap()                         } So how can i get the index after random spinning degrees?
1
0
781
Dec ’21