Post

Replies

Boosts

Views

Activity

Launch an animation when a value in picker changes
Hi, I have two charts on the page and you can switch between them with the picker. Can you please tell me how can I make them show with animation? I do not know how to start the animation when I change a chart in the picker. Thank you! struct ChartsView: View {     var stackedBarData: [ToyShape] = [         .init(type: "Cube", count: 0),         .init(type: "Sphere", count: 0),         .init(type: "Pyramid", count: 1),     ]     var charts = ["Bar chart", "Area chart"]     @State private var selectedChart = "Bar chart"     var body: some View {         VStack {             if selectedChart == "Bar chart" {                 Chart {                     ForEach(stackedBarData) { shape in                         BarMark(                             x: .value("Shape Type", shape.type),                             y: .value("Total Count", shape.count)                         )                     }                 }                 .padding(.vertical, 20)             } else if selectedChart == "Area chart" {                 Chart {                     ForEach(stackedBarData) { shape in                         AreaMark(                             x: .value("Shape Type", shape.type),                             y: .value("Total Count", shape.count)                         )                     }                 }                 .padding(.vertical, 20)             }             Picker("Select a chart", selection: $selectedChart) {                 ForEach(charts, id: \.self) {                     Text($0)                 }             }             .pickerStyle(.segmented)         }         .padding(16)     } }
0
0
952
Feb ’23
Reverse animation in swiftUI
I want to make the button move up and down (with animation) on click but when I click it, It moves only up and then I have to click it again to move the button back. How can I make the button move up and then automatically back to its first position without clicking it again? @State var isAnimated: Bool = false; var body: some View { VStack { Button(action: { withAnimation(Animation.default.repeatCount(1, autoreverses: true)) { isAnimated.toggle() } }) { Text("Text") .foregroundColor(Color(red: 0.998, green: 0.369, blue: 0.369)) .font(.system(size: 33)) } .frame(width: 100, height: 100) .background(Color(.blue)) .offset(y: isAnimated ? 100 : 0) } .padding() } }`
1
0
2.7k
Oct ’22
Display fetched data in SwiftUI
Hello, I want to fetch json data from an api and then display it in a view. It is showing me an error ("Missing return in global function expected to return 'Response'") in the end of the function. How can I return the fetched data and where should I put the return statement? private func getData(from url: String) -> Response {     let task = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: {         data, response, error in         guard let data = data, error == nil else {             print("Something went wrong")             return         }                  var result: Response?         do {             result = try JSONDecoder().decode(Response.self, from: data)         }         catch {             print("failed to convert \(error)")         }         guard let json = result else {             return         }         print(json)     })     task.resume() } I call the function this way: struct ShowData: View {     @State var results: Response     var body: some View {         Text("Hello world").onAppear {            results = getData(from: url)         }     } }
1
0
2.0k
Nov ’22
Where to store API key
Hi, I want to make an app with Swift and I want to use the google translate API. Where can I store my API key safely? When I try to find an answer on google everyone just says that it is not safe to store it in the code or that it is not possible to store it safely but I can not find some real examples of where could I store it. So can you give me some real examples please? Should I store the API key in a firebase database or cloudKit database or is it safe to use keychain? Thank you in advance :)
1
0
1.4k
Dec ’22
Pass data to a child component in SwiftUI
Hello, how can I pass data to a child component in SwiftUI? I want to pass a name to a child component but there is an error in the child component (I have uploaded an image which shows the error). I have a parent component called Content_View: struct ContentView: View {     @State private var name = "Eric"     var body: some View {         ZStack {             AnotherScreen(name: $name)         }         .padding()     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } } and a child component called AnotherScreen. struct AnotherScreen: View {     @Binding public var name: String     var body: some View {         Text(name)     } } struct AnotherScreen_Previews: PreviewProvider {     static var previews: some View {         AnotherScreen(name: String)     } }
2
0
2.2k
Oct ’22