ios - UITableView 和 UICollectionView 之间同步滚动 : lag when using multiple scrollToRowAtIndexPath

标签 ios uitableview uiscrollview scroll uicollectionview

我有一个带有 collectionView (单条水平线)和下面的 tableview 的 View 。两个 View 在滚动时同步,因为它们以不同的方式显示相同的数据。事实上,如果你有这个应用程序,它就像“神奇”的应用程序。

我设法使用 UIScrollView Delegate 方法同步两个 View

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint currentOffset = scrollView.contentOffset;
    NSLog(@"scrollViewDidScroll %@ - %@", NSStringFromCGPoint(scrollView.contentOffset), NSStringFromCGPoint(self.previousScrollOffset));
    switch (self.scrollAnimation) {
        case ScrollAnimationFromCollection:
        {
            if (currentOffset.x > self.previousScrollOffset.x && (self.scrollDirection == ScrollDirectionLeft || self.scrollDirection == ScrollDirectionNone))
            {
                // NSLog(@"Change to Right!");
                self.scrollDirection = ScrollDirectionRight;
                [self.alreadySelectedIndexPaths removeAllObjects];
            }
            else if (currentOffset.x <= self.previousScrollOffset.x && (self.scrollDirection == ScrollDirectionRight || self.scrollDirection == ScrollDirectionNone))
            {
                // NSLog(@"Change to Left!");
                self.scrollDirection = ScrollDirectionLeft;
                [self.alreadySelectedIndexPaths removeAllObjects];
            }
            [self moveTableView];
            break;
        }

        case ScrollAnimationFromTableView:
        {
            if (currentOffset.y - self.previousScrollOffset.y > 0 && self.scrollDirection == ScrollDirectionBottom)
            {
                self.scrollDirection = ScrollDirectionTop;
                [self.alreadySelectedIndexPaths removeAllObjects];
            }
            else if (currentOffset.y - self.previousScrollOffset.y <= 0 && self.scrollDirection == ScrollDirectionTop)
            {
                self.scrollDirection = ScrollDirectionBottom;
                [self.alreadySelectedIndexPaths removeAllObjects];
            }
            [self moveCollectionView];
            break;
        }

        default:
            break;
    }
    self.previousScrollOffset = currentOffset;
}

#pragma mark - Move actions
- (void)moveCollectionView
{
    NSIndexPath* currentIp = [[self.tableView indexPathsForVisibleRows] objectAtIndex:1];
    NSLog(@"currentIp %@", currentIp);
    if (![self.alreadySelectedIndexPaths containsObject:currentIp])
    {
        NSLog(@"Scroll to IndexPath centered!");
        [self.alreadySelectedIndexPaths addObject:currentIp];
        [self.collectionViewController.collectionView scrollToItemAtIndexPath:currentIp atScrollPosition:PSTCollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
}

- (void)moveTableView
{
    NSIndexPath* currentIp = [self.collectionViewController.collectionView indexPathForItemAtPoint:[self.horizontalContainer convertPoint:self.horizontalContainer.center toView:self.collectionViewController.collectionView]];
    NSLog(@"currentIp %@", currentIp);
    if (![self.alreadySelectedIndexPaths containsObject:currentIp])
    {
        NSLog(@"Scroll to IndexPath At Top!");
        [self.alreadySelectedIndexPaths addObject:currentIp];
        self.tableView.decelerationRate = UIScrollViewDecelerationRateFast;
        [self.tableView scrollToRowAtIndexPath:currentIp atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}

它工作得非常好,除了一件事:当我滚动 collectionview 时,由于动画 (YES) 参数,tableview 需要一些时间来滚动。如果我使用 scrollToRowAtIndexPath: 没有动画,它就像一个魅力。但是,一旦我使用动画标志,iOS 似乎(非常合乎逻辑地)将 scrollToRowAtIndexPath 排队并一次运行一次,导致动画滞后。

FWI, alreadySelectedIndexPaths 包含我已经滚动到的 indexPath 列表(我不知道这句话是否很清楚:D :D)

我查看是否有办法取消之前的订单,但除了 cancelPreviousPerformRequestsWithTarget: with cancels last performSelector 调用之外,它没有发现任何有用的东西。

你看到我怎样才能在这里获得流畅的动画吗?

感谢您的帮助。

最佳答案

每当滚动 tableView 或 collectionView 时覆盖 scrollviewDidScroll 方法。

在这里,您可以获取正在滚动的 View 的偏移量并将其转换为另一个 View 的偏移量。

然后你可以在另一个 View 上设置这个偏移量。

您不需要对偏移量或任何东西进行动画处理,因为无论如何都会在动画循环中调用委托(delegate)方法,因此只需设置偏移量就可以让它看起来像两个 View 都被动画化了。

希望这可以帮助。

关于ios - UITableView 和 UICollectionView 之间同步滚动 : lag when using multiple scrollToRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17019179/

相关文章:

ios - 如何找出坐标之间的距离?

ios - 在 Storyboard中使用自定义 tableviewcell 时,IBOutlet 为零

ios - 如何在 UIScrollView 到达底部时停止滚动

ios - 是否可以将 UICollectionView 作为一个单元进行缩放?

ios:根据前一个 View 中使用的segue,与另一个 View 不同的segue

ios - 越狱调整学习 - 带有 initWithFrame 的 UIViewController(单独文件) - 提供的代码

iphone - iOS:直接访问 MySQL 数据库?

ios - UITableView 检测最后一个单元格

ios - Swift 4 TableView 获取错误的单元格

ios - xcode, UIScrollView 保持循环滚动动画