2010-08-07 21 views
3

我應該能否重寫drawInContext()並在我的CALayer的邊界外繪製?即使我的圖層將maskToBounds設置爲NO(默認值),但我的drawInContext()會使用設置爲我的圖層邊界的剪輯進行調用,而我無法在其外繪製它。爲什麼我的CGContext被限制到界限?

我的測試層做這樣的事情:

-(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]; 
} 

這只是核心動畫層的限制? (我是否需要在此圖層之上繪製圖層?)

謝謝。

回答

4

圖層的內容無法溢出其邊界;但是,您可以通過指定在邊界外延伸的框架並將masksToBounds屬性設置爲NO(默認值)來添加溢出的子圖層。

相關問題