如何检测UITableView的拖动结束事件? [英] How to detect the drag end event of an UITableView?

查看:244
本文介绍了如何检测UITableView的拖动结束事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 UITableView 的拖动结束时收到通知。

I need to get notified when an UITableView's drag has come to an end.

但是我我正在处理UITableView的类别,所以我不能使用 scrollViewDidEndDragging:willDecelerate:来存档它。

But I'm working on an UITableView's category, so I can't use scrollViewDidEndDragging:willDecelerate: to archive this.

我尝试使用KVO观察拖动关键路径:

I tried use KVO to observe on dragging Key Path:

[self addObserver:self forKeyPath:@"dragging" options:NSKeyValueObservingOptionNew context:nil];

但是 observeValueForKeyPath:ofObject:change:context:没有被调用,因为UITableView.dragging没有和setter,并且这个属性不符合KVO。

But observeValueForKeyPath:ofObject:change:context: didn't get called, since UITableView.dragging doesn't have and setter and this property is not compliant with KVO.

是否有任何其他方法可以归档此预期使用 scrollViewDidEndDragging:willDecelerate:

Is there any other method to archive this expect for using scrollViewDidEndDragging:willDecelerate:?

任何帮助都很感激!谢谢!

Any help is grateful! Thanks!

推荐答案

编辑:下面我的解决方案是第一个想到的事情,结果证明在Apple决定更改 UIScrollView 类的内部时,使用起来可能不安全。请参阅Mazyod建议的答案,这应该更安全,更直接。

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.

这是依赖于实现的,可能会在未来的iOS更新中被Apple更改,但目前 UIScrollView 类似乎依赖用于管理用户交互的手势识别器和作为滚动视图类的子类的 UITableView 也是如此。

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.

如果你去了UIKit框架的UIScrollView.h,你会注意到一个可疑的 _pan ivar,它有一个 id 类型,但似乎实际上是一个 UIPanGestureRecognizer

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];

拖动表格视图时,手势识别器会多次更改,当您停止拖动时,会收到其最后一次更改为 UIGestureRecognizerStateEnded 的值。

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.

请注意,虽然这似乎可以解决问题,但其他一些问题可能会阻碍你。覆盖类别中的现有类方法通常不是一个好主意,因为此后原始实现变得不可访问。有关 <$的文档c $ c> NSKeyValueObserving 非正式协议声明

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提供NSKeyValueObserving协议的实现为所有对象提供自动观察功能。

NSObject provides an implementation of the NSKeyValueObserving protocol that provides an automatic observing capability for all objects.

因此,如果覆盖 observeValueForKeyPath:ofObject:change:context :在一个类别中,默认实现将无法访问(我们无法确定 UITableView UIScrollView 不要使用KVO的东西)。这可能会导致一些意外错误。

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.

这篇关于如何检测UITableView的拖动结束事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆