告诉滚动型等后平移手势来滚动(Tell ScrollView to Scroll after oth

2019-07-17 21:45发布

我遇到了麻烦(表视图中),滚动型滚动。 基本上,我有一个if语句,其检查是否水平运动大于垂直运动更大。 如果是,泛手势的细胞执行。 否则,我想实现代码如下滚动正常。 我一直在使用的某种变体试图self.scrollview.enabled = yes ,但我无法得到它的工作。

它工作正常的水平平移手势,但我不能让它在其他部分正常滚动。 这里是最相关的代码:(抱歉,如果这是可怕的 - 我还是新到iOS /目标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");
    }
}

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

Answer 1:

我不是真正喜欢UITableView ,但我想这个问题是分配您的自定义UIPanGestureRecognizer_tableView你基本上是无效的默认值。 反正不管你能解决这样的原因。

让我们假设你在ViewController中所做的一切,即使是很肮脏。

让你的ViewController符合的UIGestureRecognizerDelegate协议

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

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

由于文件说:

问委托如果两个手势识别器应该被允许同时识别手势。

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

这样既平底锅识别器和默认UITableView一个运行。

你需要做的最后一件事就是设置视图控制器为代表的UIPanGestureRecognizer

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

注:这是fastes和最肮脏的解决方案,您可以实现。 一个更好的解决办法是在手势跟踪逻辑移位到细胞本身,或子类UIPanGestureRecognizer 看看这个答案太。



文章来源: Tell ScrollView to Scroll after other pan gesture