我有一个ViewController,它有一个垂直滚动的集合视图,占用了整个视图。我希望能够在整个集合视图上(不仅仅是在单元格上)进行滑动和平移手势,但我无法进行任何手势。我尝试将手势识别器添加到视图和集合视图中,但似乎都不起作用。
将手势识别器添加到视图
self.panEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
self.panEdgeGesture.delegate = self;
[self.collectionView addGestureRecognizer:self.panEdgeGesture];
[self.panEdgeGesture setEdges:UIRectEdgeRight];
然后我添加了这些功能:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch{
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (void)handlePan:(UISwipeGestureRecognizer *)sender
{
DebugLog(@"Received pan gesture");
}
集合视图单元格是否可以阻止手势事件触发?他们自己没有手势。
答案 0 :(得分:1)
每UIScreenEdgePanGestureRecognizer's Class Reference:
创建屏幕边缘平移手势识别器后,指定一个 附加手势之前对edges属性的适当值 识别你的观点。您可以使用此属性指定从哪个 边缘可以开始手势。这个手势识别器忽略任何 超越第一次接触。
所以将代码更改为:
self.panEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc];
[self.panEdgeGesture setEdges:UIRectEdgeRight];
initWithTarget:self action:@selector(handlePan:)];
self.panEdgeGesture.delegate = self;
[self.collectionView addGestureRecognizer:self.panEdgeGesture];