UICollectionView滚动性能很糟糕

时间:2016-03-22 03:09:05

标签: ios objective-c xcode uicollectionview smooth-scrolling

我的collectionView中有大约14个项目,滚动根本不顺畅。我正在使用自动布局(我的约束不记录错误),我正在使用cornerRadius属性(它是一个消息传递应用程序)。

我尝试使用Time Profiler工具对其进行分析,这就是我在滚动时看到的内容:

enter image description here

不幸的是"在Xcode中显示"是灰色的,所以我无法确切地看到正在走的是什么线。我正在计算每个项目的大小。我想知道9377毫秒的巨大延迟是多少?如果我没记错的话,我没有使用有序套装。

这是我的cellForItemAtIndexpath方法:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    //Define Message
    Message *message = (Message *)[self.messages objectAtIndex:indexPath.item];

    //Check Message Type
    switch (message.type) {
        case MessageTypeText: {

            //Initialize Cell
            TextMessageItem *cell = (TextMessageItem *)[collectionView dequeueReusableCellWithReuseIdentifier:kTextMessageItemIdentifier forIndexPath:indexPath];

            //Return Cell
            return cell;

            break;
        }
    }

    return nil;
}

以下是我如何计算身高:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    //Retrieve the right message
    Message *message = (Message *)[self.messages objectAtIndex:indexPath.row];

    //Check Message Type
    switch (message.type) {
        case MessageTypeText: {

            //Create a temporary cell to get the correct size
            TextMessageItem *tempCell = [[TextMessageItem alloc] init];

            //Configure the cell
            [self collectionView:collectionView configureTextCell:tempCell atIndexPath:indexPath];

            CGSize s = [tempCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize withHorizontalFittingPriority:UILayoutPriorityDefaultHigh verticalFittingPriority:UILayoutPriorityDefaultLow];

            //Return Content Size
            return CGSizeMake(self.collectionView.bounds.size.width, s.height);

            break;
        }
    }

    //Return
    return CGSizeMake(0, 0);
}

我的WillDisplayCell方法:

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {

    //Retrieve message from our array of messages
    Message *message = (Message *)[self.messages objectAtIndex:indexPath.row];

    //Check Message Type
    switch (message.type) {
        case MessageTypeText: {

            //Cast Cell
            TextMessageItem *textItem = (TextMessageItem *)cell;
            [self collectionView:collectionView configureTextCell:textItem atIndexPath:indexPath];

            break;
        }
    }
}

最后,这是我的自定义configureTextCell方法:

- (void)collectionView:(UICollectionView *)collectionView configureTextCell:(TextMessageItem *)item atIndexPath:(NSIndexPath *)indexPath {

    //Retrieve Message
    Message *message = (Message *)[self.messages objectAtIndex:indexPath.item];

    //Display Timestamp if Needed
    [self displayTimestampForItemIfNeeded:item atIndex:indexPath.item];

    //Update Top Message Padding Based On Who Sent The Previous Message
    [self updateDistanceFromPreviousMessageForItem:item atIndex:indexPath.item];

    //Display Delivery Status if Needed (e.g. Delivered)
    [self displayDeliveryStatusForItemIfNeeded:item atIndex:indexPath.item];

    //Set Message
    [item setMessage:message];
}

1 个答案:

答案 0 :(得分:0)

使用圆角半径或图层蒙版是一种杀死滚动性能的好方法,因为合成更加昂贵。相反,您可以使用带圆角的CAShapeLayer来支持视图或作为视图的子图层。或者,您可以使用圆角将内容绘制到添加到视图中的图像中。您还应避免使用视图剪辑到边界。 (我在手机上输入此信息,远离我的电脑,因此我现在无法粘贴任何代码段。)