0

CoreGraphics と CALayers を使用して UIButton に派手なグラフィックを描画しようとしていますが、サブレイヤーに何も表示できません。

サブレイヤーの設定方法は次のとおりです。

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        CALayer *buttonLayer = self.layer;
        buttonLayer.masksToBounds = YES;
        buttonLayer.backgroundColor = [[UIColor clearColor] CGColor];
        self.myDelegate =  [[PlayerButtonLayerDelegate alloc] init];

        CALayer *customDrawn = [CALayer layer];
        customDrawn.delegate = self.myDelegate;
        customDrawn.frame = buttonLayer.frame;
        customDrawn.masksToBounds = YES;
        [buttonLayer insertSublayer:customDrawn atIndex:0];
        [customDrawn setNeedsDisplay];
    }
    return self;
}

PlayerButtonLayerDelegate は次のdrawLayer: inContext:ように実装します。

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
    CGRect rectangleFrame = layer.bounds;
    UIGraphicsPushContext(context);
    UIBezierPath* rectanglePath = 
        [UIBezierPath bezierPathWithRect: rectangleFrame];
    [[UIColor redColor] setFill];
    [rectanglePath fill];
    UIGraphicsPopContext();
}

コードが呼び出されます (ブレークポイントを設定したり、NSLog に出力したりできます) が、何も表示されません。ボタン テキストの有無にかかわらず、(メイン レイヤーで示されるように) 透明なボタンがあります。

サブレイヤーで「描画」するには何を変更する必要がありますか?

4

1 に答える 1

4
 [customDrawn setFrame:buttonLayer.bounds];
于 2012-06-06T22:22:06.780 に答える