代码之家  ›  专栏  ›  技术社区  ›  Fantattitude

如何为我的UIView子类创建CGGradient?

  •  1
  • Fantattitude  · 技术社区  · 17 年前

    有人知道如何创建一个 梯度 这将填满我的视野,
    我当前的代码是这个,它填充了 我想要一个红色的长方形 坡度

    - (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);  
    }
    
    3 回复  |  直到 10 年前
        1
  •  3
  •   Community Mohan Dere    9 年前

    在里面 my answer this question ,我提供了在UIView中绘制光泽渐变的代码。颜色和绘图位置可以根据需要进行修改,以形成任何线性渐变。

        2
  •  3
  •   sebastian friedrich    15 年前

    这是一个子类,您可以在其中选择颜色

    .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
    

    #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
    
        3
  •  2
  •   Peter Hosey    17 年前

    使用 CGGradientCreateWithColorComponents 或 CGGradientCreateWithColors CGContextDrawLinearGradient 或 CGContextDrawRadialGradient 画出来。

    线性渐变将至少在垂直于渐变线的两个方向上无限延伸。径向渐变向每个方向无限延伸。为了防止渐变溢出到视图之外,可能需要使用 CGContextClipToRect .