在drawRect中向UIView添加渐变

时间:2012-06-05 20:07:15

标签: objective-c ios graphics gradient

我正在尝试在drawRect中添加CAGradientLayer,代码如下:

- (void)drawRect:(CGRect)rect {

    CAGradientLayer *gradientOverlay = [CAGradientLayer layer];
    CGColorRef grayColor = [UIColor colorWithRed:37/255.f green:37/255.f 
                                            blue:37/255.f alpha:1.0].CGColor; 
    CGColorRef blueColor = [UIColor colorWithRed:23.0/255.0 green:171.0/255.0 
                                            blue:219.0/255.0 alpha:1.0].CGColor;
    gradientOverlay.colors = [NSArray arrayWithObjects:
                              (id) grayColor,
                              (id) grayColor,
                              (id) blueColor,
                              nil];
    gradientOverlay.locations = [NSArray arrayWithObjects:
                                 [NSNumber numberWithFloat:0],
                                 [NSNumber numberWithFloat:0.4],
                                 [NSNumber numberWithFloat:1],
                                 nil];

    CGPoint startPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect));
    CGPoint endPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect));

    gradientOverlay.startPoint = startPoint;
    gradientOverlay.frame = self.bounds;
    gradientOverlay.endPoint = endPoint;

    self.layer.mask = gradientOverlay;

}

知道为什么这不起作用吗?

3 个答案:

答案 0 :(得分:2)

如何尝试此操作而不是覆盖drawRect?

+ (void) applyGradient:(NSArray *)cgColors toView:(UIView *)view {
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = view.bounds;
    gradient.colors = cgColors;
    for(CALayer *layer in view.layer.sublayers) {
        if([layer isKindOfClass:[CAGradientLayer class]]) return;
    }
    [view.layer insertSublayer:gradient atIndex:0];
}

答案 1 :(得分:1)

CAGradientLayer添加为self.layer的子图层。
注意:如果您使用drawRect:方法进行操作,请确保每次调用CAGradientLayer时都不会添加新的drawRect:

更新(关于评论): 以下是您要问的代码:

    //Create a layer that holds your background image and add it as sublayer of your self.layer
    CALayer *layer = [CALayer layer];
    layer.frame = self.layer.frame;
    layer.contents = (id)[UIImage imageNamed:@"background.png"].CGImage;
    [self.layer addSublayer:layer];

//Create your CAGradientLayer
    CAGradientLayer *gradientOverlay = [CAGradientLayer layer];
    CGColorRef grayColor = [UIColor colorWithRed:37/255.f green:37/255.f 
                                            blue:37/255.f alpha:1.0].CGColor; 
    CGColorRef blueColor = [UIColor colorWithRed:23.0/255.0 green:171.0/255.0 
                                            blue:219.0/255.0 alpha:1.0].CGColor;
    gradientOverlay.colors = [NSArray arrayWithObjects:
                              (id) grayColor,
                              (id) grayColor,
                              (id) blueColor,
                              nil];
    gradientOverlay.locations = [NSArray arrayWithObjects:
                                 [NSNumber numberWithFloat:0],
                                 [NSNumber numberWithFloat:0.4],
                                 [NSNumber numberWithFloat:1],
                                 nil];

    CGPoint startPoint = CGPointMake(CGRectGetMinX(rect), CGRectGetMidY(rect));
    CGPoint endPoint = CGPointMake(CGRectGetMaxX(rect), CGRectGetMidY(rect));

    gradientOverlay.startPoint = startPoint;
    gradientOverlay.frame = self.layer.frame;
    gradientOverlay.endPoint = endPoint;
//set its opacity from 0 ~ 1
    gradientOverlay.opacity = 0.6f;
//add it as sublayer of self.layer (it will be over the layer with the background image
    [self.layer addSublayer:gradientOverlay];

注意:您不必在drawRect:方法中执行此操作。例如,如果您需要更改某些视图/图层的几何图形覆盖init方法,请在layoutSubviews方法中创建图层层次结构。

答案 2 :(得分:0)

通常,只有在使用CoreGraphics绘制视图时才应覆盖drawRect:

如果您要在代码中创建此视图,则放置代码的更好位置将在init;如果您将此视图放在.xib中,则awakeFromNib