在其他平移手势后告诉ScrollView滚动 [英] Tell ScrollView to Scroll after other pan gesture

查看:126
本文介绍了在其他平移手势后告诉ScrollView滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having trouble getting a scrollview (within a table view) to scroll. Basically, I have an if statement which checks if horizontal movement is greater than vertical movement. If it is, a pan gesture is executed on the cell. Else, I would like the tableview to scroll as normal. I've tried using some variation of self.scrollview.enabled = yes but I couldn't get it working.

它适用于水平平移手势,但我不能让它在else部分正确滚动。这是最相关的代码:(对不起,如果它很糟糕 - 我还是iOS / Objective C的新手)。哦,如果你在代码提取中随机看到一个奇怪的'代码',请忽略它 - 我在格式化时遇到了一些问题,我丢了一个字。

It works fine for the horizontal pan gesture, but I can't get it to scroll properly in the else section. Here's the most relevant code: (sorry if it's terrible - I'm still new to iOS/Objective C). Oh and if you see a strange 'code' randomly in the code extract, ignore it - I had some trouble formatting and I lost a word.

-(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");
    }
}

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

Thanks for the help, all suggestions are very welcome.

推荐答案

我真的不喜欢 UITableView 但我猜问题是将自定义 UIPanGestureRecognizer 分配给 _tableView 您基本上使默认值无效。无论如何你可以用这种方式解决它。

I'm not really fond on UITableView but I guess the problem is that assigning your custom UIPanGestureRecognizer to _tableView you're basically invalidating the default one. Anyway whatever the reason you can solve it this way.

让我们假设你在ViewController中做了所有事情,即使它很脏。

Let's assume you do everything in the ViewController, even if it's quite dirty.

使您的ViewController符合 UIGestureRecognizerDelegate 协议

Make your ViewController conform the 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.

返回值

是的,允许gestureRecognizer和otherGestureRecognizer同时识别他们的手势。默认实现返回NO-没有两个手势可以同时识别。

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 将运行一个。

This way both your pan recognizer and the default UITableView one will run.

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

Last thing you need to do is to set the ViewController as the delegate for the UIPanGestureRecognizer:

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

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

Note: This is the fastes and dirtiest solution you could implement. A better solution could be to shift the gesture tracking logic into the cell itself, or subclass the UIPanGestureRecognizer. Take a look at this answer too.

这篇关于在其他平移手势后告诉ScrollView滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆