2014-07-08 47 views
0

我從人們可以在設置中配置的內容創建預覽。他們可以在設置中選擇一種顏色。UIView漸變不會在第二次保存時更改

我的看法層次:

  • 設置(選擇樹的顏色,預覽樹)
  • 選擇樹的顏色(這裏,用戶可以選擇樹的顏色,用保存按鈕)

我有一個unwindSegue爲我的保存按鈕,它調用setupCell方法。 但我UIView的背景漸變層不想改變。

代碼:

- (IBAction)setupCellSegue:(UIStoryboardSegue *)segue{ 
    [self setupCell]; 
} 

-(void)setupCell{ 
    //Corners 
    self.Preview.layer.cornerRadius = 25; 
    self.Preview.layer.masksToBounds = YES; 

    //Background 
    self.Preview.backgroundColor = [UIColor clearColor]; 

    CAGradientLayer *grad = [CAGradientLayer layer]; 

    grad.frame = self.Preview.bounds; 
    grad.colors = [[[Kleuren sharedInstance] kleurenArray] objectForKey:[standardDefaults objectForKey:@"Kleur"]]; 

    [self.Preview.layer insertSublayer:grad atIndex:0]; 

    //Text 
    self.optionOutlet.textColor = [Kleuren.sharedInstance.tekstKleurenArray objectForKey:[standardDefaults objectForKey:@"TekstKleur"]]; 
} 

編輯: 這裏是從我的觀點的子層NSLog。我如何用舊的CAGradientLayer替換舊的?嘗試這樣:

[self.Preview.layer replaceSublayer:0 with:grad]; 

(
    "<CAGradientLayer: 0xcce9eb0>", 
    "<CALayer: 0xccef760>" 
) 

編輯2: 嘗試更多一些,我注意到,在剛剛添加的CAGradientLayer並添加頂部後?也許這是問題?

(
    "<CAGradientLayer: 0x170220100>", 
    "<CAGradientLayer: 0x170220e20>", 
    "<CAGradientLayer: 0x170029f40>", 
    "<CALayer: 0x17803d960>" 
) 

編輯3: 景觀層次,從而在屏幕截圖中強調了View是一個與背景。 View

編輯4:

嘗試這樣做:但現在沒有一個CALayer的了:

[[self.Preview layer] replaceSublayer:[[[self.Preview layer] sublayers] objectAtIndex:0] with:grad]; 

所以我會得到以下錯誤:

2014-07-08 18:57:18.365 ///[3324:60b] View hierarchy unprepared for constraint. 
    Constraint: <NSIBPrototypingLayoutConstraint:0xe88e0b0 'IB auto generated at build time for view with fixed frame' UILabel:0xe88dae0.left == UIView:0xe88da50.left + 20> 
    Container hierarchy: 
<UIView: 0xe88da50; frame = (59 54; 202 202); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0xe88dab0>> 
    | <CAGradientLayer: 0xe88cd50> (layer) 
    View not found in container hierarchy: <UILabel: 0xe88dae0; frame = (20 20; 162 162); text = 'Option'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0xe88db90>> 
    That view's superview: NO SUPERVIEW 

因此,它似乎是因爲CALayer被刪除,UILabel給出了這個錯誤,因爲它沒有超級觀點。

回答

0

發現了它,我已經把我的CAGradientLayer *grad;{}

之間我實現聲明通過將宣告我實現我的班。我可以重新使用聲明,並且不要每次都將它放入setupCell中來創建一個新實例。

@implementation class { 
    CAGradientLayer *grad 
} 

-(void)setupCell{ 
    // Do stuff here with the gradient layer. 
} 

我不知道這是正確的解釋,但它的工作原理。是我最後一件事了!

+0

你能解釋一下嗎?你是說你把'grad「的聲明移到了'setupCell'方法之外? – ravron

+0

Ofcourse,嘗試編輯它好一點! – k3tzor

+0

我會推薦使用一個屬性而不是一個實例變量(這就是你在這裏做的)。 – ravron

0

嘗試setupCell後加入

[self.Preview.layer setNeedsDisplay]; 

。這應該重繪圖層

+0

感謝您的評論,但它仍然無法正常工作。我注意到了一些事情,並編輯了我的問題。你能看看嗎? – k3tzor

+0

這一切都發生在您的viewController? 這裏的視圖的層次結構是什麼?什麼是setupCell行事? – gillyD

+0

正如你可以看到視圖在我的TableView單元格下,我已經使用Storyboard層次結構編輯了我的帖子 – k3tzor

0

不能馬上確定你的問題是什麼。

跳出來的第一件事就是在你的第一個編輯,你誤用replaceSublayer:with:。請參閱the documentation for that method for details,但基本上當您需要通過CALayer時,您使用了整數字面值 - 我很驚訝編譯器沒有抱怨。該方法的聲明是:

- (void)replaceSublayer:(CALayer *)oldLayer with:(CALayer *)newLayer 

要正確使用此方法,您需要保留對漸變圖層的引用。也許創建一個名爲grad的新屬性並將其存儲在那裏。稍後,當您要替換它時,請將self.grad轉換爲replaceSublayer:with:,作爲oldLayer

您已經修復了之後,這裏有一些事情要檢查:

  • 確實,當你認爲它setupCell被調用?在那裏設置一個斷點來檢查。

  • 你確定當你重新計算漸變圖層時,顏色已經改變了嗎?每次通過setupCell時,請使用斷點來檢查grad.colors的值。

+0

'setupCell'被正確調用,因爲在同一個方法中,我改變了我的標籤顏色,並且改變了我想要的。此外,顏色更新,只是檢查grad.colors,他們是oke。當我在導航中彈出一個視圖控制器並返回時,顏色將會改變。請檢查我的新編輯 – k3tzor

相關問題