[英]why UICollectionView not calling didSelectItemAtIndexPath any more after scrolling the scrollView of it's cells?
I am using the UICollectionView
to display some products. 我正在使用
UICollectionView
来显示一些产品。 In the custom UICollectionViewCell
, there is a custom UIScrollView
, where there are some images which allow the users to do a quick preview. 在自定义
UICollectionViewCell
,有一个自定义UIScrollView
,其中包含一些允许用户进行快速预览的图像。
To forward tap gesture from the UIScrollView
to the UICollectionViewCell
, i override the touches related methods as below: 要将点击手势从
UIScrollView
转发到UICollectionViewCell
,我重写了与触摸相关的方法,如下所示:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.dragging) {
NSLog(@"Began ==>");
[self.nextResponder touchesBegan:touches withEvent:event];
}
else {
[super touchesBegan:touches withEvent:event];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.dragging) {
NSLog(@"Moved ==>");
[self.nextResponder touchesMoved:touches withEvent:event];
}
else {
[super touchesMoved:touches withEvent:event];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.dragging) {
NSLog(@"Ended ==>");
[self.nextResponder touchesEnded:touches withEvent:event];
}
else {
[super touchesEnded:touches withEvent:event];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.dragging) {
NSLog(@"Cancelled ==>");
[self.nextResponder touchesCancelled:touches withEvent:event];
}
else {
[super touchesCancelled:touches withEvent:event];
}
}
sometimes it doesn't work in some cases. 有时在某些情况下不起作用。 For example, when the
UICollectionView
is loaded first time, didSelectItemAtIndexPath
method can be called when you tap, but after some scrolls or taps, the method is not called anymore, even if you try to reload the UICollectionView
, it's still not working. 例如,当第一次加载
UICollectionView
时,可以在单击时调用didSelectItemAtIndexPath
方法,但是在进行一些滚动或点击后,即使您尝试重新加载UICollectionView
,该方法也不再被调用,它仍然无法正常工作。
I try to log the message in the touch methods of UICollectionViewCell
, the touches & event is forwarded. 我尝试将消息记录在
UICollectionViewCell
的touch方法中,将touches和event转发。 But why the didSelectItemAtIndexPath
of UICollectionView
is called if the cell get the gesture? 但是,如果单元格获得手势,
UICollectionView
要调用UICollectionView
的didSelectItemAtIndexPath
?
Any advice would be much appreciated. 任何建议将不胜感激。
UPDATE: 1.the UICollectionViewCell
is loaded from nib file. 更新:
UICollectionViewCell
是从nib文件加载的。
It's not a good idea to use nextResponder if you're targeting a specific object. 如果您要针对特定对象,那么使用nextResponder并不是一个好主意。 As you've just witnessed, the responder chain can change and you may not always get the object you expect.
就像您亲眼所见,响应者链可能会更改,并且您不一定总能获得所需的对象。 Why this is happening here can be any number of reasons, but you should use a more reliable means in this situation anyways.
发生这种情况的原因可能有多种,但无论如何,在这种情况下,您都应该使用更可靠的方法。
Since UIScrollView
is a subview of the UICollectionViewCell
you can just pass the event directly to your superview [self.superview touchesMoved:touched withEvent:event];
由于
UIScrollView
是UICollectionViewCell
的子视图,因此您可以将事件直接传递给您的[self.superview touchesMoved:touched withEvent:event];
视图[self.superview touchesMoved:touched withEvent:event];
You need to keep view hierarchy in mind when you're doing this and may have to call the superview's superview to reach the cell. 执行此操作时,您需要牢记视图层次结构,并且可能必须调用超级视图的超级视图才能到达单元格。
Also I wouldn't recommend using touches to intercept user interactions. 我也不建议使用触摸来拦截用户交互。 I would highly recommend using UIGestureRecognizers as that will provide better more consistent functionality.
我强烈建议使用UIGestureRecognizers,因为它将提供更好,更一致的功能。
You should always call the super methods. 您应该始终调用super方法。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSLog(@"Began ==>");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
NSLog(@"Moved ==>");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
NSLog(@"Ended ==>");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
NSLog(@"Cancelled ==>");
}
Actually it's because of self.dragging
understanding. 其实这是因为
self.dragging
理解力不足。 The correct way to forward the tap gesture is as below: 向前轻击手势的正确方法如下:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.dragging) {
[self.nextResponder touchesEnded:touches withEvent:event];
}
else {
[super touchesEnded:touches withEvent:event];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesCancelled:touches withEvent:event];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.