2010-09-07 82 views
4

我爲我的自定義UIButton創建了CAGradientLayer。創建它的代碼如下:如何將CAGradientLayer旋轉180度?

CAGradientLayer *gradient = [CAGradientLayer layer]; 

gradient.frame = btn.bounds; 
gradient.cornerRadius = 10.0f; 
locations=[[NSArray alloc] initWithObjects: LOC_0, LOC_5, LOC_51,LOC_1, nil]; 
[gradient setLocations:locations]; 
colorNext=[[NSArray alloc] initWithObjects:(id) G3_rgb1, (id) G3_rgb2, (id) G3_rgb3, (id) G3_rgb4, nil]; 
gradient.colors = colorNext; 

[btn.layer insertSublayer:gradient atIndex:0]; 

我的問題是:在按鈕上,我需要180度改變按鈕的梯度視圖的新聞,我應該怎麼辦呢?

回答

2

只要你保持的指針梯度層,你應該能夠通過提供一種倒置組位置扭轉漸變顏色排序:如果你有

NSArray *newLocations = [[NSArray alloc] initWithObjects: [NSNumber numberWithFloat:(1.0 - [LOC_0 floatValue])], [NSNumber numberWithFloat:(1.0 - [LOC_5 floatValue])], [NSNumber numberWithFloat:(1.0 - [LOC_51 floatValue])], [NSNumber numberWithFloat:(1.0 - [LOC_1 floatValue])], nil]; 
[gradient setLocations:newLocations]; 
[newLocations release]; 

[gradient setNeedsDisplay]; 

(這將是清潔器LOC_0的浮點值等)

我不確定是否需要-setNeedsDisplay,但它通常用於圖層中的內容更改。這就是說,Vanya應用旋轉變換的解決方案可能是實現這種效果的最快捷方式。

作爲一個評論,希望你有一個[locations release][colorNext release]在你的代碼稍後的地方,否則你會泄漏的位置和顏色數組。

17

您還可以更改漸變的開始點和結束點。默認startPoint是{0.5, 0.0}。默認的endPoint是{0.5, 1.0}。翻轉這些以使您的漸變變成另一種方式。

[gradient setStartPoint:CGPointMake(0.5, 1.0)]; 
[gradient setEndPoint:CGPointMake(0.5, 0.0)]; 

翻轉它們以正常顯示。