Skip to content

Commit

Permalink
compare CGFloat in certain decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
Evelyn-Tang committed Sep 27, 2024
1 parent 9334542 commit 9906d8b
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct AppSearchModalContentView: View {
var body: some View {

ObservableScrollView { point in
if point != .zero {
if !CGPoint.compare(point, .zero, decimalPlaces: 5) {
onScroll(point)
}
} content: {
Expand Down Expand Up @@ -148,3 +148,27 @@ struct AppSearchModalContentView_Previews: PreviewProvider {
return .init(text: "Shortcut \(index + 1)", icon: .landmark, onShortcutSelected: { })
}
}

private extension CGFloat {
// Function to round a CGFloat to a specified number of decimal places
func roundToDecimal(places: Int) -> CGFloat {
let divisor = pow(10.0, CGFloat(places))
return (self * divisor).rounded() / divisor
}
}

private extension CGPoint {
// Function to round a CGPoint to a certain number of decimal places
func roundToDecimal(_ point: CGPoint, decimalPlaces: Int) -> CGPoint {
let roundedX = point.x.roundToDecimal(places: decimalPlaces)
let roundedY = point.y.roundToDecimal(places: decimalPlaces)
return CGPoint(x: roundedX, y: roundedY)
}

// Function to compare two CGPoint values with precision
static func compare(_ lhs: CGPoint, _ rhs: CGPoint, decimalPlaces: Int) -> Bool {
let roundedPoint1 = lhs.roundToDecimal(lhs, decimalPlaces: decimalPlaces)
let roundedPoint2 = rhs.roundToDecimal(rhs, decimalPlaces: decimalPlaces)
return roundedPoint1 == roundedPoint2
}
}

0 comments on commit 9906d8b

Please sign in to comment.