[英]why UICollectionView not calling didSelectItemAtIndexPath any more after scrolling the scrollView of it's cells?
我正在使用UICollectionView
来显示一些产品。 在自定义UICollectionViewCell
,有一个自定义UIScrollView
,其中包含一些允许用户进行快速预览的图像。
要将点击手势从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];
}
}
有时在某些情况下不起作用。 例如,当第一次加载UICollectionView
时,可以在单击时调用didSelectItemAtIndexPath
方法,但是在进行一些滚动或点击后,即使您尝试重新加载UICollectionView
,该方法也不再被调用,它仍然无法正常工作。
我尝试将消息记录在UICollectionViewCell
的touch方法中,将touches和event转发。 但是,如果单元格获得手势, UICollectionView
要调用UICollectionView
的didSelectItemAtIndexPath
?
任何建议将不胜感激。
更新: UICollectionViewCell
是从nib文件加载的。
如果您要针对特定对象,那么使用nextResponder并不是一个好主意。 就像您亲眼所见,响应者链可能会更改,并且您不一定总能获得所需的对象。 发生这种情况的原因可能有多种,但无论如何,在这种情况下,您都应该使用更可靠的方法。
由于UIScrollView
是UICollectionViewCell
的子视图,因此您可以将事件直接传递给您的[self.superview touchesMoved:touched withEvent:event];
视图[self.superview touchesMoved:touched withEvent:event];
执行此操作时,您需要牢记视图层次结构,并且可能必须调用超级视图的超级视图才能到达单元格。
我也不建议使用触摸来拦截用户交互。 我强烈建议使用UIGestureRecognizers,因为它将提供更好,更一致的功能。
您应该始终调用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 ==>");
}
其实这是因为self.dragging
理解力不足。 向前轻击手势的正确方法如下:
- (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.