2017-02-27 33 views
3

我的客戶想要背景使用此漸變效果查看 背景漸變:rgb(118,118,118)| #ffffff | RGB(198198197)線性左至右 我已經試過這種方式,但它是在垂直方向上發生的事情,我希望它在水平方式背景漸變:在目標中線性從左到右C

UIColor *leftColor = [UIColor colorWithRed:118.0/255.0 green:118.0/255.0 blue:118.0/255.0 alpha:1.0]; 
UIColor *middleColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]; 
UIColor *rightColor = [UIColor colorWithRed:198.0/255.0 green:198.0/255.0 blue:197.0/255.0 alpha:1.0]; 

// Create the gradient 
CAGradientLayer *theViewGradient = [CAGradientLayer layer]; 
theViewGradient.colors = [NSArray arrayWithObjects: (id)leftColor.CGColor, (id)middleColor.CGColor,(id)rightColor.CGColor, nil]; 
theViewGradient.frame = self.view.bounds; 
//Add gradient to view 
[self.view.layer insertSublayer:theViewGradient atIndex:0]; 

像這樣? enter image description here

+3

[梯度setStartPoint:CGPointMake(0,0 )]; [梯度對setEndpoint:CGPointMake(0,1)]; 加載開始點和結束點 –

+0

@ harshal帷幔它幫助 theViewGradient.startPoint = CGPointMake(0.0,0.0); theViewGradient.endPoint = CGPointMake(1.0,0.0); 感謝 –

回答

10

您需要設置gradientLayer的startPoint和endPoint屬性。它們代表第一種顏色的開始座標和最後一種顏色的結束座標。

它們都是CGPoints及其x和y應該0.0 1.0等之間的值。

默認情況下的startPoint具有這些座標(0.5,0.0),而端點具有那些(0.5,1.0)。

(0.0,0.0)爲左上角,而(1.0,1.0)是右下角

所以嘗試:

theViewGradient.startPoint = CGPointMake(0.0, 0.5); 
theViewGradient.endPoint = CGPointMake(1.0, 0.5); 
+0

感謝的人,它幫助:) –

+0

接受我的答案是否能解決您的問題 – Ocunidee

0
**The start and end points of the gradient when drawn into the layer's 
coordinate space. The start point corresponds to the first gradient 
stop, the end point to the last gradient stop. Both points are 
defined in a unit coordinate space that is then mapped to the 
layer's bounds rectangle when drawn. (i.e. [0,0] is the bottom-left 
corner of the layer, [1,1] is the top-right corner.).The default values 
are [.5,0] and [.5,1] respectively.** 

theViewGradient.startPoint = CGPointMake(0.0, 0.5); 
theViewGradient.endPoint = CGPointMake(1.0, 0.5);