Keyboard dismiss animation has influence on gesture position in Swift

24 Views Asked by At

Used this library to handle keyboard actions

Link to my source code

I have UITableView which is moving/collapsed/expanded and a bottom bar which moves alongside with a keyboard. When the keyboard is shown user can click outside the text field to hide the keyboard

The problem is UITableView receives didSelectRow events but the touch location is incorrect. Looks like table waits for keyboard dismiss animation and only then handles touches (when cells are already moved).

How to make UITableView receive correct touch location in this case?

1

There are 1 best solutions below

0
Gargo On

Doesn't work:

tableView.delaysContentTouches = false
scrollView.delaysContentTouches = false //because scrollView contains tableView

+delayed resignFirstResponder to allow UITableView to handle touches faster than keyboard will be dismissed. Result - some table sometimes ignores touches.

Works:

Modify the library code:

  1. prevent touches from passing through multiple views automatically:
tapGesture.cancelsTouchesInView = true
  1. UIResponder.currentFirstResponder?.resignFirstResponder()

This line starts animation and modifies frames immediately. So you need somehow get touch location and pass it to the view you need before it will be executed. The view can be added by passing inside delegate method (scrollView is used for other purposes and is not suitable for this case) and touches can be simulated like:

tableView?.touchesBegan([touch], with: nil)
tableView?.touchesEnded([touch], with: nil)