Skip to content

Commit

Permalink
add shake animation when expression is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
antingle committed May 6, 2022
1 parent a66b3e7 commit 09c795b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
14 changes: 14 additions & 0 deletions Calculator/Helpers/ViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,17 @@ extension Button {
)
}
}

// Used as an invalid submission shake animation
// https://www.objc.io/blog/2019/10/01/swiftui-shake-animation/
struct Shake: GeometryEffect {
var amount: CGFloat = 10
var shakesPerUnit = 3
var animatableData: CGFloat

func effectValue(size: CGSize) -> ProjectionTransform {
ProjectionTransform(CGAffineTransform(translationX:
amount * sin(animatableData * .pi * CGFloat(shakesPerUnit)),
y: 0))
}
}
9 changes: 9 additions & 0 deletions Calculator/Views/CalculatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ struct CalculatorView: View {
@State private var solution: Double = 0.0 // solution after expression evalutation
@State private var historyIndex: Int = -1 // keeps track of place in history when cycling
@State private var expressionIsValid: Bool = false // used to show live solution
@State private var invalidSubmission: Bool = false // used to shake the textfield
@State private var shouldMoveCursorToEnd: Bool = true // move cursor to end when adding to expression
@State var attempts: Int = 0

var body: some View {

Expand Down Expand Up @@ -66,6 +68,7 @@ struct CalculatorView: View {
solution = 0
}
})
.modifier(Shake(animatableData: CGFloat(invalidSubmission ? 1 : 0)))

// MARK: - Live Solution View
Text(expressionIsValid ? solution.removeZerosFromEnd() : " ")
Expand Down Expand Up @@ -114,6 +117,12 @@ struct CalculatorView: View {
expression += "("
onExpressionSubmit()
}
else {
withAnimation {
invalidSubmission = true
}
}
invalidSubmission = false
solution = 0
}
}
Expand Down
3 changes: 3 additions & 0 deletions Calculator/Views/HistoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ struct HistoryView: View {
.onTapGesture {
expression += item.expression
}
// MARK TODO: Make this an option?
// .padding(.bottom, 2)
// .overlay(Rectangle().frame(width: nil, height: 0.4, alignment: .bottom).foregroundColor(Color.gray), alignment: .bottom)

}
.padding(.bottom, 4)
Expand Down
2 changes: 1 addition & 1 deletion Calculator/Views/InfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct InfoView: View {
**Welcome to Menu Bar Calc!**
- Global shortcut ⌘⌥C
- Add previous solutions with arrow keys
- Give Feedback [here](https://github.com/ingleanthony/menu-bar-calculator/discussionsr)
- Feel free to give feedback [here](https://github.com/ingleanthony/menu-bar-calculator/discussionsr)
**Constants**
π or pi
Expand Down
1 change: 0 additions & 1 deletion Calculator/Views/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ struct SettingsView: View {
var body: some View {
VStack(alignment: .leading, spacing: 10) {


// MARK: - Toggle Buttons
Toggle(isOn: $showingButtons, label: {
Text("Show Buttons").padding(.leading, 4)
Expand Down

0 comments on commit 09c795b

Please sign in to comment.