让ScrollView在其他平移手势之后滚动

我无法获得滚动视图(在表视图内)滚动。 基本上,我有一个if语句来检查水平运动是否大于垂直运动。 如果是,则在该单元上执行平移手势。 否则,我希望tableview正常滚动。 我试过使用self.scrollview.enabled = yes一些变化,但我无法得到它的工作。

它适用于水平平移手势,但我不能让它在其他部分正确滚动。 下面是最相关的代码:(抱歉,如果这很糟糕 – 我还是iOS / Objective C的新手)。 哦,如果你在代码提取中随机地看到一个奇怪的“代码”,那么忽略它 – 我有一些麻烦的格式,我失去了一个字。

 -(void)handlePan:(UIPanGestureRecognizer *)panGestureRecognizer { CGPoint location = [panGestureRecognizer locationInView:_tableView]; //Get the corresponding index path within the table view NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:location]; TVTableCell *cell = [_tableView cellForRowAtIndexPath:indexPath]; CGPoint translation = [panGestureRecognizer translationInView:cell]; // Check for horizontal gesture if (fabsf(translation.x) > fabsf(translation.y)) { CGPoint originalCenter = cell.center; if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) { NSLog(@"pan gesture started"); } if (panGestureRecognizer.state == UIGestureRecognizerStateChanged) { // translate the center CGPoint translation = [panGestureRecognizer translationInView:self.view]; if (translation.x > 0) { cell.center = CGPointMake(originalCenter.x + (translation.x-(translation.x-3)), originalCenter.y); } else { cell.center = CGPointMake(originalCenter.x + (translation.x-(translation.x+3)), originalCenter.y); } // determine whether the item has been dragged far enough to initiate a delete / complete //this will be implemented eventually // _deleteOnDragRelease = self.frame.origin.x < -self.frame.size.width / 2; NSLog(@"state changed"); } if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) { // the frame this cell would have had before being dragged CGRect originalFrame = CGRectMake(0, cell.frame.origin.y, cell.bounds.size.width, cell.bounds.size.height); // if (!_deleteOnDragRelease) { // if the item is not being deleted, snap back to the original location [UIView animateWithDuration:0.2 animations:^{ cell.frame = originalFrame; } ]; } } else { //else: scroll tableview normally NSLog(@"dear god act normally"); } } 

感谢您的帮助,所有build议都非常欢迎。

我不是真的喜欢UITableView但我想问题是分配您的自定义UIPanGestureRecognizer _tableView你基本上是无效的默认。 无论如何,无论你以这种方式解决这个问题。

假设你在ViewController中做了一切,即使它很脏。

使您的ViewController符合UIGestureRecognizerDelegate协议

在您的ViewController.m覆盖gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:方法。

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

正如文档所述:

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

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

这样,你的泛识别器和默认的UITableView将会运行。

你需要做的最后一件事是将ViewController设置为UIPanGestureRecognizer的委托:

 UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; panRecognizer.delegate = self; 

注意:这是您可以实现的最快和最肮脏的解决scheme。 更好的解决scheme可以是将手势跟踪逻辑转换为单元本身,或者将UIPanGestureRecognizer子类UIPanGestureRecognizer 看看这个答案了。