2013-04-10 49 views
1

我有一個使用PocketSVG從SVG文件製作的UIBezierPath。如何重新調整和放置UIBezierPath?

我想包括我在CGShapeLayer UIBezierPath,並使用此代碼:

self.centerIconShapeLayer = [CAShapeLayer layer]; 
self.centerIconShapeLayer.frame = CGRectMake(5, 8, self.frame.size.width - 10, self.frame.size.height - 16); 
self.centerIconShapeLayer.fillColor = [[UIColor redColor] CGColor]; 
self.centerIconShapeLayer.contentsRect = CGRectMake(600, 0, 100, 100); 
self.centerIconShapeLayer.contentsGravity = kCAGravityCenter; 
[self.layer addSublayer:self.centerIconShapeLayer]; 
self.centerIconShapeLayer.path = myBezierPath.CGPath; 

這似乎並沒有工作,雖然,和我的外形並不就該改變大小或位置的任何這些設置。

有沒有什麼我失蹤,在功能方面將其移入位置?

回答

1

contentsGravity等功能是關於圖層的contents。形狀層的contents而不是path; path不受這些東西的影響。我建議你改用一個變換。或者,不要使用形狀圖層:只要將內容直接繪製到圖層的contents(讓我知道是否需要有關如何做到這一點的更多信息)。

另外,您正在使用contentsRect錯誤。它不是用點來衡量的,而是按比例的(每個點的座標在0到1之間 - 好吧,好吧,這比這更復雜,但這是基本的想法)。

的例子,我試過,只是爲了讓你開始(這是一個視圖控制器,你將不得不去適應一切,我在這裏做!):

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    self.centerIconShapeLayer = [CALayer layer]; // ordinary layer 
    self.centerIconShapeLayer.frame = 
     CGRectMake(5, 8, self.view.frame.size.width - 10, self.view.frame.size.height - 16); 
    self.centerIconShapeLayer.delegate = self; 
    [self.view.layer addSublayer:self.centerIconShapeLayer]; 
    // self.centerIconShapeLayer.contentsRect = CGRectMake(0, 0, 0.3, 0.3); 
    [self.centerIconShapeLayer setNeedsDisplay]; 
} 

-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { 
    UIBezierPath* myBezierPath = 
     [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,0,100,100) cornerRadius:20]; 
    CGContextAddPath(ctx,myBezierPath.CGPath); 
    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor); 
    CGContextFillPath(ctx); 
} 

運行這一點,你會看到紅色的圓角矩形。現在取消註釋contentsRect一行,你會發現它的確可以伸展紅色圓角矩形。

當然,你將不得不取代你自己的bezier路徑;這只是一個示例,向您展示如何將貝塞爾路徑的路徑直接繪製到圖層中。

欲瞭解更多有關如何直接拉進了一層,看到我的書的這一部分:

http://www.apeth.com/iOSBook/ch16.html#_drawing_in_a_layer

約拉入情境一般如需更多信息,請參閱我的書的繪畫章,從這裏開始:

http://www.apeth.com/iOSBook/ch15.html#_paths_and_drawing

+0

我這樣做。因爲我使用PocketSVG來允許我使用矢量,所以我得到了一個UIBezier路徑來使用。我可以直接從該圖層的內容中獲取信息嗎? – Andrew 2013-04-10 23:02:40

+0

是的,堅持下去,我正在爲你編寫代碼。 – matt 2013-04-10 23:07:30