In iOS 18 the following code works to set a state variable when you hold your finger on the Text()
field (well, the ScrollView()
), but it doesn't work in iOS 26:
@State private var pressed: Bool = false
...
ScrollView {
VStack {
Text("Some text goes here")
}.frame(maxWidth: .infinity)
}
.onTapGesture {} // This is required to allow the long press gesture to be recognised
.gesture(
DragGesture(minimumDistance: 0)
.onChanged({ _ in
pressed = true
})
.onEnded({ _ in
pressed = false
})
)
.background(pressed ? .black.opacity(0.4) : .clear)
I've tried changing this to:
var dragGesture: some Gesture {
DragGesture(minimumDistance: 0)
.onChanged({ _ in self.pressed = true })
.onEnded({ _ in self.pressed = false })
}
...
ScrollView {
VStack {
Text("Some text goes here")
}.frame(maxWidth: .infinity)
}
.gesture(dragGesture)
.background(pressed ? .black.opacity(0.4) : .clear)
And this:
var longPress: some Gesture {
LongPressGesture(minimumDuration: 0.25)
.onChanged({ _ in self.pressed = true })
.onEnded({ _ in self.pressed = false })
}
...
ScrollView {
VStack {
Text("Some text goes here")
}.frame(maxWidth: .infinity)
}
.gesture(longPress)
.background(pressed ? .black.opacity(0.4) : .clear)
Neither works.
Any ideas? Thanks.