0

私の要件は、テキストの一部をUITableviewの各セル内に太字で色付きで表示することです。NSMutableAttributedString、CATextLayer、およびセルのcontenviewのレイヤーを使用して管理しました。以下は私が表示するために使用したコードです。

 CATextLayer *textLayer = nil;   //used to draw the attributed string
 CALayer *layer = nil; 
    if(cell == nil) {
                 textLayer = [[CATextLayer alloc] init];
                textLayer.backgroundColor = [UIColor clearColor].CGColor;
                [textLayer setForegroundColor:[[UIColor clearColor] CGColor]];
                [textLayer setContentsScale:[[UIScreen mainScreen] scale]];
                textLayer.wrapped = YES;
                layer = cell.contentView.layer;
                [layer addSublayer:textLayer];
                textLayer = nil;
    }
//get the layer by nesting sublayer of cell layer because we don't have tag property for the layer and textlayer.


if (!layer) {
       layer = cell.contentView.layer;
   }



for (id subLayer in layer.sublayers) {
            if ([subLayer isKindOfClass:[CATextLayer class]]) {
                // NSLog(@"found textlayer");
                textLayer = subLayer;
                textLayer.string = nil;
            }
        }
textLayer.frame = CGRectMake(53.0f, 23.0f, 240.0f, attributedStrHeight);

        if([self isNotNull:mAttrbtdStr]) {
            textLayer.string = mAttrbtdStr;
        }

問題は、テーブルを高速でスクロールすると、テキストがアニメーション化(オーバーラップ)として表示され、スクロールを停止した後、正しく表示されることです。以下の参考画像をご覧ください

テーブルの高速スクロール中にAttributedStringが重複している

テーブルをスクロールしたときにのみこの重複が発生する理由を教えてください。

4

1 に答える 1

1

それは本当に興味深い質問です。CALayerプロパティはデフォルトでアニメーション化されているため(暗黙のアニメーション)、スクロールビューアニメーション内のデフォルトの動作になる可能性がありますCATextLayer。1 つの解決策は、それらを無効にすることです。2 つのオプションがあります。

  1. 現在のトランザクションでアニメーションを無効にしますが、新しいトランザクションを作成している場合でも、これがテーブルビューの通常のアニメーションにどのように影響するかはわかりません

    [CATransaction begin];

    [CATransaction setValue: [NSNumber numberWithBool: YES] forKey: kCATransactionDisableActions];

    [CATransaction commit];

  2. アニメーション アクション ディクショナリを null を返すように設定します

于 2012-06-18T13:35:02.057 に答える