在我的應用程序中,我有一個視圖,我希望它在左側和右側有一個小的透明漸變。我可以很輕鬆地創建左邊或右邊的梯度,但我不知道如何將它們結合起來:CALayer面具 - 結合兩層
CAGradientLayer *rightLayer = [CAGradientLayer layer];
rightLayer.frame = self.bounds;
rightLayer.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor, nil];
rightLayer.startPoint = CGPointMake(0.8f, 1.0f);
rightLayer.endPoint = CGPointMake(1.0f, 1.0f);
CAGradientLayer *leftLayer = [CAGradientLayer layer];
leftLayer.frame = self.bounds;
leftLayer.colors = [NSArray arrayWithObjects:(id)[UIColor clearColor].CGColor,(id)[UIColor whiteColor].CGColor, nil];
leftLayer.startPoint = CGPointMake(0.0f, 1.0f);
leftLayer.endPoint = CGPointMake(0.2f, 1.0f);
self.layer.mask = leftLayer;
正如你所看到的,問題是,我必須指派只有1層掩蓋,我想它有2層。我如何合併leftLayer和rightLayer?或者我如何添加這兩個圖層來掩蓋?
非常感謝您的回答!
嗯......我已經試過了。我設置顏色,但是當我不能將cgpoints添加到位置時......我該怎麼做? –
@animal_chin添加一個這樣的NSValue:'[NSValue valueWithCGPoint:myCGPoint];' –
完成了它!最終解決方案如下所示: CAGradientLayer * combined = [CAGradientLayer layer]; combined.frame = self.bounds; (id)[UIColor whiteColor] .CGColor,(id)[UIColor whiteColor] .CGColor,(id)[UIColor clearColor] .CGColor,nil(組合顏色) ]。 combined.startPoint = CGPointMake(0.0f,1.0f); combined.endPoint = CGPointMake(1.0f,1.0f); 也許設置位置會改善一點,但對我來說這工作得很好:) –