在drawInRect中绘制渐变层

Posted

技术标签:

【中文标题】在drawInRect中绘制渐变层【英文标题】:Draw gradient layer in drawInRect 【发布时间】:2017-05-11 10:34:47 【问题描述】:

我正在尝试开发一个 costum 视图,它的复合版本包含一个包含渐变层的 costum UIView。现在我想在我的新服装视图的 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?

我的复合实现工作正常,但这个不行。我曾尝试跟随,但它是无限循环:

- (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);

【问题讨论】:

【参考方案1】:

使用渐变层或在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 的级别稍低,因此您可能不会在不创建循环的情况下使用其中的大部分层调用。

【讨论】:

以上是关于在drawInRect中绘制渐变层的主要内容,如果未能解决你的问题,请参考以下文章

iOS 绘制颜色渐变圆环

有角度的渐变层

Swift UITableView中的渐变层

如何停止使用渐变 UICollectionCell 创建新图层?覆盖 func prepareForReuse()

canvas怎么画一个渐变的圆角边框,填充的也行

如何修复不一致的颜色到清晰的径向渐变层