2016-11-04 22 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