将垂直滚动手势传递给下面的UITableView

(为清晰起见编辑)

我有一个UITableView。 最重要的是附有平移手势的UIView。 这个Pan向左和向右滑动来改变底层的表格。 我使用平移手势的动作方法来移动表格。 这工作正常。

但是,UIView及其平移手势会干扰向上/向下滚动UITableView。 我怎样才能发送上/下滚动到桌子上,并保持在视图的区域左右?

--------------------------------------- | | | ---------------------- | | | | | | | | | | | | | | UITableView | | | | | UIView | | | | + | | | | PanGesture | | | | | | | | | | | | | | | | | | | ---------------------- | | | | | --------------------------------------- 

由泛手势触发的方法是这样的

  -(void)move:(UIPanGestureRecognizer*)sender { CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view]; float xTest = fabsf(translatedPoint.x); float yTest = fabsf(translatedPoint.y); if ( xTest>yTest) { // Move table view left-right.. this works } else { // Send up-down scrolling gesture to table view????? How to? } } 

我刚刚解决了类似的问题,除了是垂直平底锅而不是水平平底锅。 我不是100%确定你的用例,所以这可能不是你想要的,但它可能会导致你在正确的方向。

我分类了UIPanGestureRecognizer,并实现了touchesMoved方法,并检查手势是否有较大的水平或垂直变化。 下面是一个片段。 该信用属于不同的计算器后,但目前我找不到链接。 (对不起,格式不好,第一次发布)

 -(void)touchesMoved:(NSSet*) touches withEvent:(UIEvent *)event { [super touchesMoved:touches withEvent:event]; if(self.state == UIGestureRecognizerStateFailed) return; CGPoint currentPoint = [[touches anyObject] locationInView:self.view]; CGPoint prevPoint = [[touches anyObject] previousLocationInView:self.view]; moveX += prevPoint.x - currentPoint.x; moveY += prevPoint.y - currentPoint.y; if(!drag) { if(abs(moveY) > abs(moveX)) drag = YES; else self.state = UIGestureRecognizerStateFailed; } } -(void)reset { [super reset]; drag = NO; moveX = 0; moveY = 0; } 

在我的父视图控制器,我相信这将是UITableView在这种情况下,我也实现了以下。 我认为就你而言,如果是横向平移,你就不想返回。

 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if([gestureRecognizer isKindOfClass:[VerticalPanGestureRecognizer class]]) { return YES; } return NO; } 

让我知道如果有任何不清楚。

祝你好运!

UIGestureRecognizer有一个属性,可以阻止识别的手势将其事件转发到视图层次 – 听起来好像这是您的正确select。 尝试一下,并在你的手势识别器上设置为NO

cancelsTouchesInView

一个布尔值,影响识别手势时是否将触摸传递给视图。

 @property(nonatomic) BOOL cancelsTouchesInView 

讨论

当此属性为YES(默认值)并且接收方识别其手势时,该待处理手势的触摸将不会传送到视图,而先前传送的触摸将通过touchesCancelled:withEvent:消息发送到视图来取消。 如果手势识别器不识别其手势,或者如果此属性的值为NO,则视图接收多点触摸序列中的所有触摸。

在iOS 3.2及更高版本中可用。

来自UIGestureRecognizer参考 。

好的,答案是将平移添加到UITableView而不是UIView。 我检查手势的位置是否在(现在隐藏在xib中)UIView的边界内,如下所示:

  - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer { CGPoint translation = [gestureRecognizer locationInView:self.view]; CGRect frame = self.slideTarget.frame; if (CGRectContainsPoint(frame, translation)) { return YES; } else { return NO; } } 

繁荣。

我明白,你想要在UIView手势根据其方向来处理(垂直 – >滚动表,水平 – >滚动UIView)。 我读了 – 不是尝试 – 关于这个:UISwipeGestureRecognizer有一个属性的方向 – 从文档:

 The permitted direction of the swipe for this gesture recognizer. 

也许这可以帮助你?

编辑:哦,好吧,我没有认识到你正在看一个UIPanRecognizer(太多“刷卡”那里)…也许你可以通过手势到桌子时,平移的方向是“足够垂直”,据报道通过translationInView: ..?

在你的平移手势的方法写这个:

 - (void)pan:(UIPanGestureRecognizer *)gesture { CGPoint translatedPoint = [gesture translationInView:self.tableView]; if (ABS(translatedPoint.y) > 10.0f) { self.tableView.bounds = CGRectMake(0, -translatedPoint.y, 320, 460); } if (ABS(translatedPoint.x) > 10.0f) { // swipe left/right code... } } 

在这里你通过改变它的边界属性来模仿你的表视图的滚动。 但是这个代码不会反弹表视图。 这种效果可以通过在平移手势开始时保存反弹属性状态,然后在手势结束时分配该属性来实现。

正如我可以从你的需求中得知,当你正在交互另一个视图时,你需要滚动表格。 所以查看图层会像这样:

UINavigationController – >视图(导航控制器将有UITableViewController)UITableViewController(它将有UiTableView)

最后

UIView(UINavigationController.view子视图UIView)

所以你想,如果你平移(滚动)你的UIView,那么你的TableView将正确滚动。

所以你需要做一个你需要的UIView类(让我们假设CustomView)。 然后在您的CustomView类中实现以下方法。

 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *hitView = [super hitTest:point withEvent:event]; // If the hitView is THIS view, return the view that you want to receive the touch instead: if (hitView == self) { return _tableView; } else if ([hitView isKindOfClass:[UIButton class]]) { return hitView; } return _tableView; } 

如果你的CustomView也有一个button子视图。 那么你也会得到select。

请原谅我的不好的解释,但我只是find了解决scheme,我想与你分享。

让我知道如果你需要更多的解释。 我会稍后解释。

实现名为- gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:UIGestureReconizerDelegate方法- gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

它默认返回false ,执行它并返回true.

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return true; } 

而且,不要忘记设置手势的delegate属性。

 // Set the delegate of the panGesture. For example self.panGestureReconizer.delegate = ... // 

从苹果文档

(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

询问代表是否应允许两个手势识别器同时识别手势。

返回值

YES允许gestureRecognizer和其他KeepRender识别器同时识别他们的手势。 默认实现返回NO-不能同时识别两个手势。

讨论

当由gestureRecognizer或其他GestureRecognizer识别手势将阻止其他手势识别器识别其手势时,将调用此方法。 请注意,返回YES保证允许同时识别; 另一方面,返回“否”不能保证防止同时识别,因为另一个手势识别器的委托人可能返回YES。


阅读更多