Trying to create a function that increments a value, then returns the longPress gesture. Receiving the following
Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type
Reason behind the function would be because I have three buttons that will increase on increments of one.
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)
}
}
CodePudding user response:
Because of the multiple let
variables, the view does not know what to display/ return. Put a return
before the outer VStack to fix that.
Then remove some curly braces and fix your longPressAction
function.
This should help: https://www.hackingwithswift.com/books/ios-swiftui/how-to-use-gestures-in-swiftui.
Fixed code:
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)
}
}
}