在drawInRect中绘制渐变层

Draw gradient layer in drawInRect

我正在尝试开发一个 costum 视图,它的复合版本包含一个包含渐变层的 costum UIView。现在我想在我的新 Costum 视图的 drawInRect 函数中绘制这个图层,但我不确定我应该怎么做。

我以前的带有渐变图层的自定义视图:

@interface ViewWithGradientLayer : UIView

@property (nonatomic, strong, readonly) CAGradientLayer *layer;

@end

@implementation ViewWithGradientLayer

@dynamic layer;

+ (Class)layerClass {
    return [CAGradientLayer class];
}

@end

当我想使用这个视图时,我正在做:

UIColor *startColor = [UIColor colorWithRed:0.00 green:0.00 blue:0.02 alpha:1.0];
UIColor *endColor = [UIColor clearColor];
self.gradientBackground.layer.colors = @[(__bridge id) endColor.CGColor, (__bridge id) startColor.CGColor];
[self.gradientBackground.layer setStartPoint:CGPointMake(0.5, 0.0)];
[self.gradientBackground.layer setEndPoint:CGPointMake(0.5, 1.0)];

现在我已经添加了相同的图层 属性 到我想在 drawInRect 中绘制图层的新自定义视图:

@interface UIBPVCell : UICollectionViewCell

@property (nonatomic, strong, readonly) CAGradientLayer *layer;

@end

@implementation UIBPVCell

@dynamic layer;

+ (Class)layerClass {
    return [CAGradientLayer class];
}

- (void)commonInit {
    UIColor *startColor = [UIColor colorWithRed:0.00 green:0.00 blue:0.02 alpha:1.0];
    UIColor *endColor = [UIColor clearColor];
    self.layer.frame = newsImageRect;
    self.layer.colors = @[(__bridge id) endColor.CGColor, (__bridge id) startColor.CGColor];
    [self.layer setStartPoint:CGPointMake(0.5, 0.0)];
    [self.layer setEndPoint:CGPointMake(0.5, 1.0)];
}

- (instancetype)initWithFrame:(CGRect)frame {
    NSLog(@"UIBPVCell : initWithFrame");
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    //How to draw layer here?
}

我的复合实现工作正常,但这个不行。我试过 followwing 但会无限循环:

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self.layer drawInContext:UIGraphicsGetCurrentContext()];
}

更新:

我已经找到了与所选答案相似的答案。这是我的解决方案:

- (void)drawRect:(CGRect)rect {
    NSLog(@"UIBPVCell : drawRect");
    [super drawRect:rect];

    if (_newsImage) {
        [_newsImage drawInRect:newsImageRect];

    } else [placeHolderImage drawInRect:newsImageRect];

    CGFloat colors [] = {
            0.0f, 0.0f, 0.0f, 0.0f, //Start color: Clear color
            0.0f, 0.0f, 0.00f, 0.5f //End color: Black with alpha
    };

    CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 2);
    CGColorSpaceRelease(baseSpace);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGPoint startPoint = CGPointMake(CGRectGetMidX(newsImageRect), CGRectGetMinY(newsImageRect));
    CGPoint endPoint = CGPointMake(CGRectGetMidX(newsImageRect), CGRectGetMaxY(newsImageRect));
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    CGGradientRelease(gradient);
}

使用渐变层或在draw rect中绘制渐变是完全不同的。要在 drawRect 中绘制渐变,您应该尝试如下操作:

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    UIColor *startColor = [UIColor colorWithRed:1.0f green:.0f blue:.0f alpha:1.0f];
    UIColor *endColor = [UIColor colorWithRed:.0f green:1.0f blue:.0f alpha:1.0f];

    NSArray *colors = @[(__bridge id)startColor.CGColor, (__bridge id)endColor.CGColor];
    CGGradientRef gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), (CFArrayRef)colors, nil);

    CGContextDrawLinearGradient(UIGraphicsGetCurrentContext(), gradient, CGPointMake(0.0f, 0.0f), CGPointMake(self.bounds.size.width, self.bounds.size.height), kCGGradientDrawsBeforeStartLocation|kCGGradientDrawsAfterEndLocation);
}

覆盖 drawRect 的级别较低,因此您可能无法在不创建循环的情况下使用其中的大部分图层调用。