2011-03-24 59 views
1

我正在用CAGradient圖層創建一個簡單的欄。我有6種顏色,我希望在圖層上均勻分佈。瞭解CAGradient位置?

NSMutableArray *colors = [NSArray arrayWithObjects: 
          (id)[[UIColor colorWithRed:0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:.95] CGColor], 
          (id)[[UIColor colorWithRed:204.0/255.0 green:153.0/255.0 blue:0/255.0 alpha:.95]CGColor], 
         (id)[ [UIColor colorWithRed:204.0/255.0 green:102.0/255.0 blue:51.0/255.0 alpha:.95]CGColor], 
         (id)[ [UIColor colorWithRed:204.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:.95]CGColor], 
         (id)[ [UIColor blackColor]CGColor],nil]; 

NSMutableArray *locations = [NSArray arrayWithObjects: 
           [NSNumber numberWithFloat:0.0], 
           [NSNumber numberWithInt:0.14], 
           [NSNumber numberWithInt:0.28], 
           [NSNumber numberWithInt:0.42], 
           [NSNumber numberWithInt:0.56], 
           [NSNumber numberWithInt:0.70], 
           [NSNumber numberWithInt:0.84], 
           [NSNumber numberWithInt:1.0],nil]; 

,並實現它們:

gradientLayer_.colors =colors; 
gradientLayer_.locations=locations; 

但我得到一個完全的黑條。當我刪除的位置,我看到的梯度完美,但它沒有很好地劃分。

我覺得我不太瞭解位置。

有幫助嗎?

回答

3
  1. 您在幾個停靠位置定義上使用了[NSNumber numberWithInt:]。使用[NSNumber numberWithFloat:]
  2. 顏色的數量應該與站點的數量相同。你有5種顏色和8個停止位置。添加3種顏色或刪除3個停止位置。
+0

謝謝。你知道我發現在90%的情況下,它總是一個非常簡單的解決方案。 – shannoga 2011-03-24 15:43:37