如何使用滑动手势在 swift UI 中创建退格功能(与原始 IOS 计算器相同)
How to create backspace Functionality in swift UI with a swipe Gesture (Same as in original IOS calculator)
我正在尝试删除单个数字,在数字上滑动(向左或向右,哪个方向无关紧要。)每次滑动都会删除另一个数字,直到我们达到零。
您可以在下面找到我正在努力实现的附加图片。谁能向我建议如何实现它?
关于如何在 swift UI 中实现此目标的任何想法?
您可以在下面找到我的代码,它负责显示字符串。
import SwiftUI
struct CalculationState {
var currentNumber: Double = 0
let characterLimit = 7
var storedNumber: Double?
var storedAction: ActionView.Action?
mutating func appendNumber(_ number: Double) {
if number.truncatingRemainder(dividingBy: 1) == 0 && currentNumber.truncatingRemainder(dividingBy: 1) == 0 {
currentNumber = 10 * currentNumber + number
} else {
currentNumber = number
}
}
}
struct Home: View {
//MARK: - Properties
@StateObject var viewModel = FetchData()
@State var state = CalculationState()
let characterLimit = 9
let firstBreakDownDivider = 3
//MARK: - Computed propety
var displayedString: String {
return String(String(format: "%.f", arguments: [state.currentNumber]).prefix(characterLimit))
}
//MARK: - Body
var body: some View {
//View of Number Pad
VStack(alignment: .trailing, spacing: 20) {
// Spacer()
Text(displayedString)
.font(.system(size: 75))
.foregroundColor(.white)
.lineLimit(1)
.padding(.bottom)
.gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local)
.onEnded({ value in
if value.translation.width < 0 {
// left
state.currentNumber = Double(displayedString.dropLast()) ?? 0
print("Swiped Left")
}
if value.translation.width > 0 {
// left
state.currentNumber = Double(displayedString.dropLast()) ?? 0
print("Swiped right")
}
}))
您可以尝试在显示您号码的视图上收听 DragGesture
。
要从数字中删除数字,一种简单的方法是将数字转换为字符串,然后对该字符串调用 dropLast()
。之后,将字符串转换回数字并更新您的视图。
我正在尝试删除单个数字,在数字上滑动(向左或向右,哪个方向无关紧要。)每次滑动都会删除另一个数字,直到我们达到零。
您可以在下面找到我正在努力实现的附加图片。谁能向我建议如何实现它?
关于如何在 swift UI 中实现此目标的任何想法?
您可以在下面找到我的代码,它负责显示字符串。
import SwiftUI
struct CalculationState {
var currentNumber: Double = 0
let characterLimit = 7
var storedNumber: Double?
var storedAction: ActionView.Action?
mutating func appendNumber(_ number: Double) {
if number.truncatingRemainder(dividingBy: 1) == 0 && currentNumber.truncatingRemainder(dividingBy: 1) == 0 {
currentNumber = 10 * currentNumber + number
} else {
currentNumber = number
}
}
}
struct Home: View {
//MARK: - Properties
@StateObject var viewModel = FetchData()
@State var state = CalculationState()
let characterLimit = 9
let firstBreakDownDivider = 3
//MARK: - Computed propety
var displayedString: String {
return String(String(format: "%.f", arguments: [state.currentNumber]).prefix(characterLimit))
}
//MARK: - Body
var body: some View {
//View of Number Pad
VStack(alignment: .trailing, spacing: 20) {
// Spacer()
Text(displayedString)
.font(.system(size: 75))
.foregroundColor(.white)
.lineLimit(1)
.padding(.bottom)
.gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local)
.onEnded({ value in
if value.translation.width < 0 {
// left
state.currentNumber = Double(displayedString.dropLast()) ?? 0
print("Swiped Left")
}
if value.translation.width > 0 {
// left
state.currentNumber = Double(displayedString.dropLast()) ?? 0
print("Swiped right")
}
}))
您可以尝试在显示您号码的视图上收听 DragGesture
。
要从数字中删除数字,一种简单的方法是将数字转换为字符串,然后对该字符串调用 dropLast()
。之后,将字符串转换回数字并更新您的视图。