尝试创建一个递增值的函数,然后返回longPress手势。接收以下
函数声明了一个不透明的返回类型,但没有返回语句在其体内,从中推断潜在类型
函数背后的原因是因为我有三个按钮,它们的增量为一。
struct CustomFoodItemView: View {
@State var foodName = ""
@State var proteinAmount = 1
@State var carbAmount = 1
@State var fatAmount = 1
@State private var timer: Timer?
@State var isLongPressing = false
var body: some View {
let releaseGesture = DragGesture(minimumDistance: 0) // << detects finger release
.onEnded { _ in
self.timer?.invalidate()
}
func longPressAction ( macroToIncrease: inout Int) -> some Gesture{
let longPressGesture = LongPressGesture(minimumDuration: 0.2)
.onEnded { _ in
self.timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { _ in
macroToIncrease += 1
})
}
return longPressGesture
}
let proteinCombined = longPressAction(macroToIncrease: &proteinAmount).sequenced(before: releaseGesture)
VStack{
VStack{
Text("Food Name")
TextField("", text: $foodName)
.multilineTextAlignment(.center)
.border(.white)
.padding(.trailing, 10)
.frame(width:100, height:10)
}
HStack{
Text(String(proteinAmount) + "g")
.frame(width:50, height:50)
Image(systemName: "plus.circle.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30, height: 30)
.foregroundColor(Color("SuccessButtonColor"))
.onTapGesture{
proteinAmount += 1
}
.gesture(proteinCombined)
Image(systemName: "minus.circle.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30, height: 30)
.foregroundColor(Color("SuccessButtonColor"))
}
}
}
Spacer()
.frame(maxWidth:.infinity, maxHeight: .infinity)
}
}
由于有多个let
变量,视图不知道显示/返回什么。在外部VStack之前放置一个return
来修复它。
然后去掉一些大括号并修复longPressAction
函数。这应该有助于:https://www.hackingwithswift.com/books/ios-swiftui/how-to-use-gestures-in-swiftui.
固定代码:
struct CustomFoodItemView: View {
@State var foodName = ""
@State var proteinAmount = 1
@State var carbAmount = 1
@State var fatAmount = 1
@State private var timer: Timer?
@State var isLongPressing = false
var body: some View {
let releaseGesture = DragGesture(minimumDistance: 0) // << detects finger release
.onEnded { _ in
self.timer?.invalidate()
}
let longPressGesture = LongPressGesture(minimumDuration: 0.2)
.onEnded { _ in
self.timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { _ in
proteinAmount += 1
})
}
let proteinCombined = longPressGesture.sequenced(before: releaseGesture)
return VStack {
VStack {
Text("Food Name")
TextField("", text: $foodName)
.multilineTextAlignment(.center)
.border(.white)
.padding(.trailing, 10)
.frame(width: 100, height: 10)
}
HStack {
Text(String(proteinAmount) + "g")
.frame(width: 50, height: 50)
Image(systemName: "plus.circle.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30, height: 30)
.foregroundColor(Color("SuccessButtonColor"))
.onTapGesture {
proteinAmount += 1
}
.gesture(proteinCombined)
Image(systemName: "minus.circle.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30, height: 30)
.foregroundColor(Color("SuccessButtonColor"))
}
Spacer()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
}