2016-11-04 70 views
0

這裏我使用此代碼給出漸變效果,但根本沒有獲得效果。添加CAGradient圖層但無法獲得漸變效果

CAGradientLayer *layer2 = [CAGradientLayer layer]; 

NSArray *gradientColors = [NSArray arrayWithObjects:(id)[self colorWithHexString:@"3eb79d"].CGColor,(id)[self colorWithHexString:@"3fb65c"].CGColor,(id)[self colorWithHexString:@"3cb7da"].CGColor, nil]; 

    [layer2 setColors:gradientColors]; 
    [layer2 setFrame:cell.userBackgroundView.layer.frame]; 
    [cell.userBackgroundView.layer insertSublayer:layer2 atIndex:0]; 
    cell.userBackgroundView.clipsToBounds = YES; 

回答

1

試試這個

CAGradientLayer *gradient = [CAGradientLayer layer]; 
gradient.frame =cell.userBackgroundView.frame; 
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor] CGColor], nil]; 
[cell.userBackgroundView.layer insertSublayer:gradient atIndex:0]; 
0

嘗試從0變更子層的指數爲-1

[cell.userBackgroundView.layer insertSublayer:layer2 atIndex:-1]; 

確保您userBackgroundView沒有覆蓋另一種看法。

0

使用

[layer2 setFrame:cell.userBackgroundView.bounds]; 

,而不是

[layer2 setFrame:cell.userBackgroundView.layer.frame]; 

如果您正在使用的autoLayout,確保設定的框架viewDidAppearviewDidLayOutSubviews