3

我无法让滚动视图(在表格视图中)滚动。基本上,我有一个 if 语句来检查水平移动是否大于垂直移动。如果是,则在单元格上执行平移手势。否则,我希望表格视图正常滚动。我试过使用一些变体,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");
    }
}

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

4

1 回答 1

8

我不是很喜欢,UITableView但我想问题在于将您的自定义分配给您UIPanGestureRecognizer基本上_tableView会使默认设置无效。无论如何,无论出于何种原因,您都可以通过这种方式解决它。

假设您在 ViewController 中执行所有操作,即使它很脏。

使您的 ViewController 符合UIGestureRecognizerDelegate协议

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

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

正如文档所说:

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

返回值
YES 以允许gestureRecognizer 和otherGestureRecognizer 同时识别它们的手势。默认实现返回 NO——不能同时识别两个手势。

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

您需要做的最后一件事是将 ViewController 设置为UIPanGestureRecognizer

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

注意:这是您可以实施的最快和最脏的解决方案。更好的解决方案可能是将手势跟踪逻辑转移到单元格本身,或者将UIPanGestureRecognizer. 也看看这个答案

于 2013-01-06T12:54:29.353 回答