在 drawRect 中向 UIView 添加渐变



我正在尝试在我的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;
}

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

尝试这个而不是覆盖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];
}

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

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

    //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方法。

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

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

相关内容

  • 没有找到相关文章

最新更新