0
我想用CoreGraphics和CALayers在UIButton上绘制一些花哨的图形,但是我无法获取任何要显示的子图层。如何在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只是实现这样的:
- (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),但没有任何显示:我只是有一个透明的按钮(如主层所示),有和没有任何按钮文本。
我必须更改为“绘制”子图层上的什么?
您应该将子图层的“frame”设置为按钮图层的“bounds”,而不是其框架。不知道这是否是唯一的问题。 – omz
Do .. :-)你可以提交这个答案,以便我能+ 1 /接受它吗?很明显,一旦有人指出!请让我知道是否还有其他问题!谢谢。 – Thorsten