在其他平移手势后告诉ScrollView滚动

mat*_*mer 3 iphone scrollview ipad ios

我无法获得滚动视图(在表格视图中)滚动.基本上,我有一个if语句,用于检查水平移动是否大于垂直移动.如果是,则在单元格上执行平移手势.另外,我希望tableview能够正常滚动.我尝试过使用一些变体,self.scrollview.enabled = yes但我无法使用它.

它适用于水平平移手势,但我不能让它在else部分正确滚动.这是最相关的代码:(对不起,如果它很糟糕 - 我还是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");
    }
}
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助,非常欢迎所有建议.

mok*_*gio 8

我不是很喜欢,UITableView但我想问题是将你的自定义分配UIPanGestureRecognizer_tableView你基本上使默认的自定义无效.无论如何,你可以用这种方式解决它.

让我们假设您在ViewController中执行所有操作,即使它非常脏.

使ViewController符合UIGestureRecognizerDelegate协议

在你的ViewController.m覆盖gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:方法中.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

正如文件所说:

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

返回值
YES允许gestureRecognizer和otherGestureRecognizer同时识别其手势.默认实现返回NO-不能同时识别两个手势.

这样,您的平移识别器和默认识别器UITableView都将运行.

您需要做的最后一件事是将ViewController设置为以下代理UIPanGestureRecognizer:

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panRecognizer.delegate = self;
Run Code Online (Sandbox Code Playgroud)

注意:这是您可以实施的最重要且最脏的解决方案.一个更好的解决方案可能是将手势跟踪逻辑转换为单元格本身,或者将其子类化UIPanGestureRecognizer.看看这个答案也是如此.