2016-03-22 10 views
0

私はcollectionViewに約14個のアイテムがあり、スクロールは全くスムーズではありません。私は自動レイアウトを使用しています(私の制約はエラーを記録しません)、私はcornerRadiusプロパティを使用しています(メッセージングアプリケーションです)。UICollectionView Scrollパフォーマンス恐ろしい

私は時間プロファイラ機器を使用して、それを分析してみた、これは私がスクロールしたときに見たものだった。

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]; 
} 
+0

私の 'cellForItemAtIndexpath'は正しく実装されていないのでしょうか?私の 'UICollectionViewCell'サブクラスはプログラム的に(ストーリーボードやxibファイルなしで)実行されています。私は初期ロード時にそれに気づいています。その' initWithFrame'メソッドはmy.messages配列の各項目に対して呼び出されています。これは正常ですか? – KingPolygon

答えて

0

コーナー半径またはレイヤーマスクを使用すると、スクロールのパフォーマンスが低下する可能性があります。これは、合成がコスト高になるためです。代わりに、あなたのビューを裏返すために、またはビューのサブレイヤとして丸い角を持つCAShapeLayerを使用することができます。または、あなたのビューに追加するイメージに丸いコーナーのコンテンツを描画することもできます。また、ビューのクリップ・ツー・バウンドを避けるべきです。 (私は自分のコンピュータから電話機にこれを入力していますので、今すぐスニペットに貼り付けることはできません)

+0

ありがとうございます。私はあなたが示唆しているいくつかの変更を試みて、顕著な違いがあるかどうかを見てみましょう。 – KingPolygon

+0

私はあなたが示唆していることを試してみましたが、それは少し助けになりますが、それはまだ目立って遅れています。プロファイラ操作が9377msから8800msに短縮されました。本当の質問は...おそらく8800msを取ることができるでしょうか?これは、scrollDidLoadではなく、スクロールするときにのみ表示されます。 – KingPolygon

+0

もう少しだと、あなたのコードを詳しく見ていきます。決してあなたがしたくないことは、prepareForReuse時間またはdequeueReusableCellWithReuseIdentifier時間の制約を変更することです。 init時にこれらを設定します。 – Mustang

関連する問題