【问题标题】:How to create a CGGradient for my UIView subclass?如何为我的 UIView 子类创建 CGGradient?
【发布时间】:2009-07-17 23:52:52
【问题描述】:

有谁知道如何创建一个 CGGradient 来填充我的视野,
我当前的代码是这样的,它用红色矩形填充 UIView 我想要一个 gradient (例如从黑色到灰色)而不是矩形:

- (void)drawRect:(CGRect)rect {
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGRect r;
    r.origin.x=0.;
    r.origin.y=0.;
    r.size.width=rect.size.width;   
    r.size.height=rect.size.height; 
    CGContextSetRGBFillColor(context, 1., 0., 0., 1.);
    CGContextFillRect (context,r);  
}

【问题讨论】:

  • 快速切线提示:如果您使用 CGRectMake,您的 rect 设置代码会更清晰。
  • 或 C99 风格的初始化程序。 CGRect r = { .origin = CGZeroRect, .size = rect.size };
  • 感谢您的想法,我将使用 CGRect 让我觉得它看起来不错 =)... C99 风格的初始化程序很好,但对我来说 Cocoa 不够:P

标签: objective-c cocoa-touch uiview core-graphics


【解决方案1】:

my answerthis question 中,我提供了在UIView 中绘制光泽渐变的代码。可以从中修改颜色和绘图位置,以形成您需要的任何线性渐变。

【讨论】:

    【解决方案2】:

    这是一个可以选择颜色的子类

    .h 文件

    #import <UIKit/UIKit.h>
    
    @interface GradientView : UIView {
        CGGradientRef gradient;
    }
    
    @property(nonatomic, assign) CGGradientRef gradient;
    
    - (id)initWithGradient:(CGGradientRef)gradient;
    - (id)initWithColor:(UIColor*)top bottom:(UIColor*)bot;
    - (void)setGradientWithColor:(UIColor*)top bottom:(UIColor*)bot;
    - (void)getRGBA:(CGFloat*)buffer;
    
    @end
    

    .m 文件

    #import "GradientView.h"
    
    @implementation GradientView
    
    @synthesize gradient;
    
    - (id)initWithGradient:(CGGradientRef)grad {
        self = [super init];
        if(self){
            [self setGradient:grad];
        }
        return self;
    }
    
    - (id)initWithColor:(UIColor*)top bottom:(UIColor*)bot {
        self = [super init];
        if(self){
            [self setGradientWithColor:top bottom:bot];
        }
        return self;
    }
    
    - (void)setGradient:(CGGradientRef)g {
        if(gradient != NULL && g != gradient){
            CGGradientRelease(gradient);
        }
        if(g != gradient){
            CGGradientRetain(g);
        }
        gradient = g;
        [self setNeedsDisplay];
    }
    
    - (void)setGradientWithColor:(UIColor*)top bottom:(UIColor*)bot {
        CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
        CGFloat clr[8];
        [top getRGBA:clr];
        [bot getRGBA:clr+4] ;
        CGGradientRef grad = CGGradientCreateWithColorComponents(rgb, clr, NULL, sizeof(clr)/(sizeof(clr[0])*4));
        [self setGradient:grad];
        CGColorSpaceRelease(rgb);
        CGGradientRelease(grad);
    }
    
    - (void)getRGBA:(CGFloat*)buffer {
        CGColorRef clr = [self CGColor];
        NSInteger n = CGColorGetNumberOfComponents(clr);
        const CGFloat *colors = CGColorGetComponents(clr);
        // TODO: add other ColorSpaces support
        switch (n) {
            case 2:
                for(int i = 0; i<3; ++i){
                    buffer[i] = colors[0];
                }
                buffer[3] = CGColorGetAlpha(clr);
                break;
            case 3:
                for(int i = 0; i<3; ++i){
                    buffer[i] = colors[i];
                }
                buffer[3] = 1.0;
                break;
    
            case 4:
                for(int i = 0; i<4; ++i){
                    buffer[i] = colors[i];
                }
                break;
            default:
                break;
        }
    }
    
    - (void)drawRect:(CGRect)rect {
        CGContextRef c = UIGraphicsGetCurrentContext();
        CGContextDrawLinearGradient(c, gradient, CGPointMake(0, 0), CGPointMake(0, rect.size.height), 0);
    }
    
    
    - (void)dealloc {
        CGGradientRelease(gradient);
        [super dealloc];
    }
    
    
    @end
    

    【讨论】:

    • 我猜 -(void)getRGBA: 方法应该属于 UIColor 类别,对吧?
    【解决方案3】:

    使用CGGradientCreateWithColorComponentsCGGradientCreateWithColors 创建渐变。 (后者采用 CGColor 对象。)然后,使用CGContextDrawLinearGradientCGContextDrawRadialGradient 进行绘制。

    线性渐变将至少在垂直于渐变线的两个方向上无限延伸。径向梯度在每个方向上无限延伸。为了防止渐变溢出到您的视图之外,您可能需要使用 CGContextClipToRect 将视图的边界添加到剪切路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 2016-06-19
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多