我有UIView
并且我附加了一个UIPanGestureRecognizer
. 在UIView
我启用了分页的情况下,我设置了内容大小,因此scrollViewUIScrollView
可以滚动到左侧。
问题:
我希望当用户尝试将 scrollView 向右拖动时,将事件发送到 UIView 以便“UIPanGestureRecognizer”可以处理触摸事件
我有UIView
并且我附加了一个UIPanGestureRecognizer
. 在UIView
我启用了分页的情况下,我设置了内容大小,因此scrollViewUIScrollView
可以滚动到左侧。
问题:
我希望当用户尝试将 scrollView 向右拖动时,将事件发送到 UIView 以便“UIPanGestureRecognizer”可以处理触摸事件
最后,在 6 小时后我发现我子类化UIScrollView
并实现了gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer
委托方法
当用户拖动滚动视图时,此方法被调用(BOOL)gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer
,默认情况下它返回 NO
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if (self.contentOffset.x > 0 && self.contentOffset.x <= self.contentSize.width - self.frame.size.width) {
//user is in middle of dragging the scrollView don't allow any other gestureRecognizer to respond
return NO;
}else{
//scrollView contentOffset is 0 and user is not dragging the scrollView so let other gestureRecognizer to respond
return YES;
}
}
在尝试使用 homam 的答案时,我意识到scrollView.panGestureRecognizer
在return NO
. 我想要的是我的自定义平移识别器被切断并且滚动视图仍然滚动。我认为这可能是在不同级别添加手势的结果,我不确定。
然而,我找到了一个解决方案,通过更改我的自定义手势识别器来检查scrollView.contentOffset
并且不激活,除非它大于零。一旦归零,我也关闭了该scrollView.bounce
属性。scrollView.contentOffset
当在我的 scrollView 上向下滚动时,它会走到尽头,我的手势识别器会立即接管并像我想要的那样将超级视图移开。
这是我的自定义平移手势的骨架
- (void)customPan:(UIPanGestureRecognizer*)panRecognizer
{
if(panRecognizer.state == UIGestureRecognizerStateBegan)
{
//do begin stuff
}
if(panRecognizer.state == UIGestureRecognizerStateChanged)
{
if(self.scrollView.contentOffset.y > 0)
{
//do begin stuff to make sure it’s initialized properly when it does start to change
self.scrollView.bounces = YES;
return;
}
else
{
self.scrollView.bounces = NO;
}
//do change stuff
}
if(panRecognizer.state == UIGestureRecognizerStateEnded)
{
if(self.scrollView.contentOffset.y > 0)
{
//do begin stuff to make sure it’s initialized properly when it does start to change
self.scrollView.bounces = YES;
return;
}
else
{
self.scrollView.bounces = NO;
}
//do end stuff
}
}
[myScrollView setContentSize:CGSizeZero];