2014-01-14 88 views
2

我有以下代碼:CAGradientLayer與CAShapeLayer面膜不顯示

- (void)setupLayer { 
    self.faucetShape = [CAShapeLayer layer]; 
    self.faucetShape.strokeColor = [[UIColor blackColor] CGColor]; 
    self.faucetShape.lineWidth = 2; 
    self.faucetShape.fillColor = nil; 
    self.faucetShape.path = [self faucetPath].CGPath; // faucetPath returns a 4 point diamond shaped UIBezierPath* 
    self.faucet = [CAGradientLayer layer]; 
    self.faucet.mask = self.faucetShape; 
    self.faucet.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor redColor].CGColor]; 
    [self.layer addSublayer: self.faucet]; 
} 

但是這說明不了什麼。不知道我錯過了什麼(除了我的圖形)。

faucetfaucetPath都是屬性)

UPDATE

OK,我想一個CAGradientLayer必須有一個有意義的frame。我假設CAShapeLayerframe沒有那麼有意義,因爲path可以超出frame。將下面的行使其顯示:

self.faucet.frame = CGRectMake(0, 0, 64, 64); 

所以現在我的問題是,有沒有辦法來避免這樣做呢?我只是想讓圖層的框架隱含地成爲包含View/Layer的框架。

+0

它們不是弱性質,它們是? – Gavin

+0

笑,不:)他們是'(強,非原子)' –

+0

你是否檢查過你的'faucetPath'方法返回你想要的? – Gavin

回答

3

您需要設置圖層的框架。以下代碼將畫出一個紅色圓圈:

- (void)setupLayer 
{ 
    CAShapeLayer *faucetShape = [CAShapeLayer layer]; 
    faucetShape.frame = CGRectMake(0, 0, 100, 100); 
    faucetShape.strokeColor = [[UIColor blackColor] CGColor]; 
    faucetShape.lineWidth = 2; 
    faucetShape.fillColor = nil; 
    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathAddEllipseInRect(path, nil, CGRectMake(0, 0, 100, 100)); 
    faucetShape.path = path; 
    CGPathRelease(path); 
    CAGradientLayer *faucet = [CAGradientLayer layer]; 
    faucet.frame = CGRectMake(10, 10, 100, 100); 
    faucet.mask = faucetShape; 
    faucet.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor redColor].CGColor]; 
    [self.view.layer addSublayer: faucet]; 
}