为什么我的 CGContext 被剪裁到边界?

发布于 2024-09-13 11:38:23 字数 1143 浏览 3 评论 0原文

我是否应该能够重写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠意 2024-09-20 11:38:23

图层的内容不可能溢出其边界;但是,您可以通过指定延伸到边界之外的框架并将 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 to NO (the default).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文