UIPageViewController 滑动延迟

UIPageViewController Swipe Delay

我的 iOS 应用程序使用了大约 18 个页面的 UIPageViewcontroller。每个页面都有一个带有 4 个动态单元格的 UITableView。每个单元格大约有 10 张不同的图像。

问题是每次我滑动UIPageViewcontroller去next/previous页面,有半秒的延迟,让用户体验很差。

滑动后显示的下一页的 UITableView 似乎正在加载其所有繁重的数据单元格,这就是造成此延迟的原因。

我读过可以使此加载异步,但我不知道如何执行此操作。

你们中的任何人都知道如何避免这种延迟以便顺利过渡到页面吗?

这是 UITableView 的一些代码:

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cellsStatus[indexPath.row] integerValue] == CLOSED_CELL)
        return [GrintScoreTrackPlayerDynamicCell getHeightForCell];
    else
        return [GrintScoreTrackPlayerDynamicCell getFullHeightForCell];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([mainController respondsToSelector:@selector(tableViewDidScroll:)] &&
        [mainController isKindOfClass:[GrintScoreTrackViewController class]])
    {
        [mainController tableViewDidScroll:scrollView];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier;
    GrintScoreTrackPlayerDynamicCell *cell, *cellOwner;

    // Cell ID
    CellIdentifier = @"GrintScoreTrackPlayerDynamicCell-ID";

    // Creating the cell
    cell = (GrintScoreTrackPlayerDynamicCell*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Creating the cell owner
    cellOwner = [[GrintScoreTrackPlayerDynamicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    // Getting cell from Nib, if possible.
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"GrintScoreTrackPlayerDynamicCell" owner:cellOwner options:nil];
        cell = [nib objectAtIndex:0];
    }

    // Filling cell with the corresponding user info
    [cell fillCellWithContent];

    // Changing appearance based on the new status
    [self setStatusOfCell:cell withStatus:[cellsStatus[indexPath.row] integerValue]];

    // Setting action for cell's "collapseButton" button
    [cell.collapseButton addTarget:self action:@selector(changeCellStatus:) forControlEvents:UIControlEventTouchUpInside];

    // Setting action for cell's "editScoresButton" button
    [cell.editScoresButton addTarget:self action:@selector(editScoresButtonWasPressed:) forControlEvents:UIControlEventTouchUpInside];

    // Setting action for cell's "pickerEnterButton" button
    [cell.pickerEnterButton addTarget:self action:@selector(pickerEnterButtonWasPressed:) forControlEvents:UIControlEventTouchUpInside];

    // Setting cell selection style
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.layoutMargins = UIEdgeInsetsZero;

    // Setting picker view delegate and data source.
    [cell.pickerView setDelegate:self];
    [cell.pickerView setDataSource:self];

    return cell;
}

还有来自 GrintScoreTrackPlayerDynamicCell 单元格的一些代码:

- (void) fillCellWithContent
{
    // Doing some processing fixes
    [self postProcessCellContent];
}

- (void) postProcessCellContent
{
    CGSize textSize;
    CGFloat scoreLabelTextWidth, newHazzardLabelXValue, newPuttsLabelXValue, offset;

    // Setting an offset for the hazzard and putts label. This offset represents the distance between the score label text and the hazzard and putts labels.
    offset = 1;

    // Getting the width of the text that's inside the Score Label.
    textSize = [[self.scoreLabel text] sizeWithAttributes:@{NSFontAttributeName:[self.scoreLabel font]}];
    scoreLabelTextWidth = textSize.width;

    // Getting new x position value for the Hazzards Label.
    newHazzardLabelXValue = self.scoreLabel.frame.origin.x +
                            (self.scoreLabel.frame.size.width - scoreLabelTextWidth) * 0.5 -
                            self.hazardsLabel.frame.size.width - offset;

    // Setting the new x position value for the Hazzards label
    [GrintUtils moveView:self.hazardsLabel inX:newHazzardLabelXValue andY:self.hazardsLabel.frame.origin.y];

    // Getting new x position value for the Putts Label.
    newPuttsLabelXValue = self.scoreLabel.frame.origin.x +
                         (self.scoreLabel.frame.size.width - scoreLabelTextWidth) * 0.5 +
                          scoreLabelTextWidth + offset;

    // Setting the new x position value for the Putts label
    [GrintUtils moveView:self.puttsLabel inX:newPuttsLabelXValue andY:self.puttsLabel.frame.origin.y];
}

这是一些关于我的 UIPageViewController 的代码,页面预加载在 pageContent 数组中:

#pragma mark - UIPageViewController Data Course

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    // Getting the previous of the next controller
    int index;
    index = currentPageBeingDisplayed - 1;
    if (index < 0)
        index = [pageContent count] - 1;

    return pageContent[index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    // Getting the index of the next controller
    int index;
    index = currentPageBeingDisplayed + 1;
    index = index % [pageContent count];

    return pageContent[index];
}

Instruments Tool Counter 的屏幕截图着重于延迟:

细胞的最后2张图片(它有2个状态):

这将是一个大胆的猜测 - 我还没有尝试过。但是从你如此精彩的推论,以及你在评论中所说的,我认为这就是图像进入细胞的方式:它们在细胞定义 .xib.

因此,对于 table 的每一行,都会加载 .xib 并且图像必须全部从 .xib 重新加载。这就是我们认为正在减慢您速度的原因。

所以这里有一个建议:不要那样做。相反,单元格中只有空图像视图,并用 cellForRowAtIndexPath:.

中的图像填充它们

为什么这会有帮助?因为如果您通过调用 imageNamed: 获取图像,则该图像 被系统缓存 。因此,每个图像将被获取一次,之后将被永久缓存。

还有一个建议 - 确保图像,即与将要显示的大小相同。从大图开始,然后在小图视图中显示它是一种巨大的时间浪费,因为每次要显示的图像版本都必须计算