Post

Replies

Boosts

Views

Activity

Issues with answer checking and out-of-bounds answers in SwiftUI game
I am building a math quiz game in SwiftUI with a liquid swipe interface where the user needs to swipe or tap the correct answer among two choices displayed on the screen. However, I'm facing several issues with the game logic and answer checking. Here are the problems I'm encountering: The game is generating answers that are out of bounds: Sometimes, when the user swipes or taps the correct side, the game recognizes an entirely different answer that is out of the valid range of answer choices. Inconsistent answer checking: The answer checking mechanism is not working reliably. There are cases where the correct side is swiped or tapped, but the game incorrectly prints "INCORRECT!" instead of "CORRECT!". In addition, when the recognized answer is out of bounds, it doesn't print anything. I suspect that the issues lie in the swipe function and the answer checking logic. I have already tried implementing various fixes suggested in online resources, but none of them have resolved the problems. There are 3 parts to the code. I have included links to the ContentView and any other necessary code: https://github.com/soltanon8/forums-question/blob/main/ContentView https://github.com/exyte/LiquidSwipe/blob/master/LiquidSwipe/SliderData.swift https://github.com/exyte/LiquidSwipe/blob/master/LiquidSwipe/WaveView.swift Could someone please help me identify the issues with the answer checking and out-of-bounds answers in my SwiftUI game? If there are any modifications I need to make in this question, please let me know with a helpful comment. Any suggestions or insights would be greatly appreciated. Thank you in advance for your assistance!
1
0
416
Jul ’23
Issue with Implementing Additional Buttons in SwiftUI Calculator
I'm working on a calculator app in SwiftUI, and I'm trying to add some additional buttons to incorporate advanced mathematical functions. I've already implemented the basic calculator functionality, but I'm facing issues with making these new buttons work as expected. The additional buttons I'm trying to implement are: Trigonometric Functions: sin, cos, tan, arcsin, arccos, arctan Exponential and Logarithmic Functions: e^x, ln, log Power and Root Functions: x^2, x^3, √x, ³√x, x^y Hyperbolic Functions: sinh, cosh, tanh, arcsinh, arccosh, arctanh Other Functions: abs, factorial, percent Could someone please guide me on how to properly integrate these buttons into the calculator functionality? Any insights or suggestions would be greatly appreciated. Here's the code I have so far, which includes the basic calculator functionality and the additional buttons I'm trying to incorporate: struct ContentView: View { @State private var input: String = "" @State private var result: String = "" var body: some View { VStack { Text(result) .font(.largeTitle) .padding() HStack { CalculatorButton(title: "sin", action: { appendToInput("sin(") }) CalculatorButton(title: "cos", action: { appendToInput("cos(") }) CalculatorButton(title: "tan", action: { appendToInput("tan(") }) CalculatorButton(title: "arcsin", action: { appendToInput("asin(") }) } HStack { CalculatorButton(title: "arccos", action: { appendToInput("acos(") }) CalculatorButton(title: "arctan", action: { appendToInput("atan(") }) CalculatorButton(title: "e^x", action: { appendToInput("exp(") }) CalculatorButton(title: "ln", action: { appendToInput("log(") }) } HStack { CalculatorButton(title: "log", action: { appendToInput("log10(") }) CalculatorButton(title: "x^2", action: { appendToInput("pow(") }) CalculatorButton(title: "x^3", action: { appendToInput("pow(") }) CalculatorButton(title: "√x", action: { appendToInput("sqrt(") }) } HStack { CalculatorButton(title: "³√x", action: { appendToInput("pow(") }) CalculatorButton(title: "x^y", action: { appendToInput("^") }) CalculatorButton(title: "sinh", action: { appendToInput("sinh(") }) CalculatorButton(title: "cosh", action: { appendToInput("cosh(") }) } HStack { CalculatorButton(title: "tanh", action: { appendToInput("tanh(") }) CalculatorButton(title: "arcsinh", action: { appendToInput("asinh(") }) CalculatorButton(title: "arccosh", action: { appendToInput("acosh(") }) CalculatorButton(title: "arctanh", action: { appendToInput("atanh(") }) } HStack { CalculatorButton(title: "π", action: { appendToInput("pi") }) CalculatorButton(title: "e", action: { appendToInput("exp(1)") }) CalculatorButton(title: "abs", action: { appendToInput("abs(") }) CalculatorButton(title: "factorial", action: { appendToInput("factorial(") }) } HStack { CalculatorButton(title: "%", action: { appendToInput("%") }) CalculatorButton(title: "+", action: { appendToInput("+") }) CalculatorButton(title: "-", action: { appendToInput("-") }) CalculatorButton(title: "*", action: { appendToInput("*") }) } HStack { CalculatorButton(title: "/", action: { appendToInput("/") }) CalculatorButton(title: ".", action: { appendToInput(".") }) CalculatorButton(title: "Clear", action: { clearInput() }) CalculatorButton(title: "=", action: { evaluateExpression() }) } } .padding() } private func appendToInput(_ value: String) { input.append(value) } private func clearInput() { input = "" result = "" } private func evaluateExpression() { let expression = NSExpression(format: input) if let evaluatedValue = expression.expressionValue(with: nil, context: nil) as? NSNumber { result = evaluatedValue.stringValue } else { result = "Invalid expression" } } } struct CalculatorButton: View { let title: String let action: () -> Void var body: some View { Button(action: action) { Text(title) .font(.title) .frame(width: 80, height: 80) .foregroundColor(.white) .background(Color.blue) .cornerRadius(40) } } }
4
0
615
Jun ’23
Preventing SwiftUI rectangles from going outside active area during drag gesture
I'm working on a SwiftUI project where I have one rectangle that I want to be draggable within an active area. However, I'm facing an issue where the rectangle can go outside the active area during the drag gesture. I want to constrain the position of the rectangle so that it stays within the active area during the drag gesture. How can I achieve this? Any insights or suggestions would be greatly appreciated. Thank you in advance! Here's a simplified version of my code: struct ContentView: View { @State var position = CGSize.zero @State var lastPosition = CGSize.zero var body: some View { ZStack { Rectangle() .fill(Color(red: 0.5450980392156862, green: 0.5450980392156862, blue: 0.5450980392156862)) .aspectRatio(0.75, contentMode: .fit) .frame(height: 550) .cornerRadius(30) .offset(x: position.width, y: position.height) .animation(.spring(response: 0.4, dampingFraction: 0.4, blendDuration: 0.4)) .gesture( DragGesture() .onChanged({ value in position = CGSize(width: lastPosition.width + value.translation.width, height: lastPosition.height + value.translation.height) }) .onEnded({ value in lastPosition = position }) ) .padding() } } }
1
0
1k
Jun ’23
Creating a Draggable Calculator Interface in SwiftUI
I am designing a unique calculator interface inspired by the stage manager, desktop experience, and Group FaceTime concepts. The interface consists of three draggable widgets that serve different purposes. The first widgets acts as the display for the calculator, providing a blank canvas for the calculations. The second widget contains scientific buttons, allowing users to perform advanced mathematical operations. The third widget consists of basic calculator buttons for standard arithmetic functions. By combining these elements, the calculator aims to provide a versatile and intuitive user experience. How can I implement a draggable interface for a calculator in SwiftUI that matches the image of my design below, where the user can interact with three separate rectangles representing the calculator display, working scientific buttons, and working basic calculator buttons? The design I'm trying to follow and the individual widgets are shown in this image: Here's a minimal SwiftUI code example that represents my attempt at the described draggable calculator interface: struct ContentView: View { @State private var displayPosition: CGSize = .zero @State private var scientificButtonsPosition: CGSize = .zero @State private var basicButtonsPosition: CGSize = .zero var body: some View { ZStack { Color.gray.opacity(0.2) .ignoresSafeArea() DraggableRectangle(position: $displayPosition, color: .white) { // Calculator Display // Customize the view as per your requirements Text("Display") .font(.title) } DraggableRectangle(position: $scientificButtonsPosition, color: .blue) { // Scientific Buttons // Customize the view as per your requirements Text("Scientific Buttons") .font(.headline) .foregroundColor(.white) } DraggableRectangle(position: $basicButtonsPosition, color: .green) { // Basic Calculator Buttons // Customize the view as per your requirements Text("Basic Buttons") .font(.headline) .foregroundColor(.white) } } .frame(minWidth: 800, minHeight: 600) } } struct DraggableRectangle<Content: View>: View { @Binding var position: CGSize let color: Color let content: () -> Content var body: some View { content() .frame(width: 400, height: 300) .background(color) .cornerRadius(8) .offset(position) .gesture( DragGesture() .onChanged { value in position = value.translation } .onEnded { _ in position = .zero } ) } }
1
0
744
Jun ’23