Edit: My solution below was the first thing to come in mind and turned out to be rather hacky and may be unsafe to use in case Apple decides to change the internals of the UIScrollView
class. See the answer suggested by Mazyod which should be safer and more straightforward.
This is implementation-dependent and may be changed by Apple in future iOS updates, but currently UIScrollView
class seems to rely on gesture recognizers for managing user interaction and UITableView
being a subclass of the scroll view class does the same.
If you go to UIScrollView.h of the UIKit framework, you can notice a suspicious _pan
ivar which has an id
type, but seems to actually be a UIPanGestureRecognizer
.
So I've tried this, and it seems to work.
[_tableView addObserver: self
forKeyPath: @"pan.state"
options: NSKeyValueObservingOptionNew
context: nil];
When dragging the table view, state
of the gesture recognizer changes several times, and when you stop dragging, state
receives its last change to the value of UIGestureRecognizerStateEnded
.
Please note that although this seems to do the trick, some other problem may stand in your way. It is generally not a good idea to override existing class methods in a category since the original implementation becomes inaccessible after that. Documentation on the NSKeyValueObserving
informal protocol states that
NSObject provides an implementation of the NSKeyValueObserving protocol that provides an automatic observing capability for all objects.
So if you override observeValueForKeyPath:ofObject:change:context:
in a category, the default implementation will be inaccessible (and we cannot be sure that UITableView
or UIScrollView
do not user KVO for something). That may cause some unexpected errors.