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

标签 iphone ios ipad scrollview

我无法滚动 ScrollView (在 TableView 中)。基本上,我有一个 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");
    }
}

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

最佳答案

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

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

让你的 ViewController 符合 UIGestureRecognizerDelegate protocol

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

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

正如文档所说:

Asks the delegate if two gesture recognizers should be allowed to recognize gestures simultaneously.

Return Value
YES to allow both gestureRecognizer and otherGestureRecognizer to recognize their gestures simultaneously. The default implementation returns NO—no two gestures can be recognized simultaneously.

这样你的 pan 识别器和默认的 UITableView 都会运行。

您需要做的最后一件事是将 ViewController 设置为 UIPanGestureRecognizer 的委托(delegate):

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

注意:这是您可以实现的最快且最脏的解决方案。更好的解决方案可能是将手势跟踪逻辑转移到单元格本身,或者子类化 UIPanGestureRecognizer。看看this answer也是。

关于iphone - 告诉 ScrollView 在其他平移手势后滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14180613/

相关文章:

ios - 仅在 UIWebView 中显示站点的一部分

iphone - 使用 UITapGestureRecognizer 而不是手动调用 tapCount

iphone - paypal的iphone支付sdk是苹果认可的吗?

ios - MC 系统组容器和 MC 从键盘外观上的公共(public)有效用户设置错误中读取

ios - 我可以禁用 Mac Catalyst iOS 应用程序上的窗口大小调整功能吗

ios - 如何检查 connectionDidFinishLoading 何时完成

html - iPad 显示有问题

iphone - Xcode 发送 HTTPS Soap 请求

iphone - 在UIView上部分设置渐变,一半颜色为渐变,一半为单色

iphone - 在应用程序外部加载YouTube