I have a UIButton
at he bottom of a chat screen. I use TouchDown
to detect that the user started holding the button and start audio recording (standard messenger app functionality).
The problem is that when the button is at the bottom of the screen, there is a 1s lag from the moment I start to hold the button to the moment TouchDown
fires.
When the keyboard is displayed and the bottom moves up the screen, there is no lag at all.
I found a similar question, iOS - Delayed "Touch Down" event for UIButton in UITableViewCell but the button is not in a table view or scrollview
Overriding the touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
method of the button and just printing the events shows that it also has the same lag.
This makes me think that the lag is related to the button position at the bottom of the screen.
Turns out, hinted by https://stackoverflow.com/a/47255802/581164, the lag is caused by iOS waiting if the user is actually performing a system gesture (like swipe from the bottom of the screen) instead of tapping the button.
If I override var preferredScreenEdgesDeferringSystemGestures: UIRectEdge
on the root view controller and set it to [.bottom, .right]
there is no lag. I do not know why [.bottom]
is not enough, I guess there is some other iOS gesture for swiping from the right edge of the screen and the button is located in the bottom right corner.
Always just setting [.bottom, .right]
has some negative effects, the user has to swipe 2x to perform the iOS swipe from bottom gesture.
The key seems to be to set it only when the user is going to press the recording button, like in func point(inside point: CGPoint, with event: UIEvent?) -> Bool
and then set it back when the user releases the button.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With