为什么我的 CGContext 被剪裁到边界?
我是否应该能够重写 drawInContext() 并在 CALayer 的边界之外进行绘制?即使我的图层的 maskToBounds 设置为 NO(默认值),我的 drawInContext() 也会被调用,并将剪辑设置为我的图层边界,但我无法在其外部进行绘制。
我的测试层做了类似的事情:
-(void)drawInContext:(CGContextRef)context
{
[super drawInContext:context];
NSLog(@"mask to bounds=%d", self.masksToBounds); // NO
CGRect clip = CGContextGetClipBoundingBox(context);
NSLog(@"clip=%f,%f,%f,%f", clip.origin.x, clip.origin.y, clip.size.width, clip.size.height); // reports the bounds
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextBeginPath(context);
CGContextSetLineWidth(context, 5.0);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 500, 0.0); // wider than my layer...
CGContextStrokePath(context);
}
这是我的设置方式:
- (void)viewDidLoad
{
[super viewDidLoad];
CALayer *layer = [[MyLayer alloc] init];
layer.needsDisplayOnBoundsChange=YES;
layer.frame = CGRectMake(50, 50, 200, 200);
[self.view.layer addSublayer:layer];
}
这只是核心动画层的限制吗? (我需要在这层之上的图层中绘制吗?)
谢谢。
Should I be able to override drawInContext() and draw outside the bounds of my CALayer? Even though my layer has maskToBounds set to NO (the default) my drawInContext() is called with a clip set to the bounds of my layer and I am unable to draw outside of it.
My test layer does something like this:
-(void)drawInContext:(CGContextRef)context
{
[super drawInContext:context];
NSLog(@"mask to bounds=%d", self.masksToBounds); // NO
CGRect clip = CGContextGetClipBoundingBox(context);
NSLog(@"clip=%f,%f,%f,%f", clip.origin.x, clip.origin.y, clip.size.width, clip.size.height); // reports the bounds
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextBeginPath(context);
CGContextSetLineWidth(context, 5.0);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 500, 0.0); // wider than my layer...
CGContextStrokePath(context);
}
and here is how I set it up:
- (void)viewDidLoad
{
[super viewDidLoad];
CALayer *layer = [[MyLayer alloc] init];
layer.needsDisplayOnBoundsChange=YES;
layer.frame = CGRectMake(50, 50, 200, 200);
[self.view.layer addSublayer:layer];
}
Is this just a limitation of core animation layers? (Do I need to draw in the layer above this layer?)
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
图层的内容不可能溢出其边界;但是,您可以通过指定延伸到边界之外的框架并将
masksToBounds
属性设置为NO
(默认值)来添加溢出的子图层。There is no way for a layer's contents to overflow its bounds; however, you can add a sublayer that overflows by specifying a frame that stretches outside the bounds and setting the
masksToBounds
property toNO
(the default).