2009-07-17 4 views
1

誰もがCGGradientを作成する方法を知っている、
私の現在のコードはこれです、それは私がグレーに黒から勾配を(持っていると思います赤枠でのUIViewを埋めます代わりに、RECTのインスタンス):私のUIViewサブクラスにCGGradientを作成するには?私の見解を記入します

- (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); 
} 
+1

クイック接線方向先端あなたが色を選ぶことができ、サブクラス

.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 

です:あなたRECTセットアップコードがあれば非常にクリーンになりますCGRectMakeを使用します。 – smorgan

+1

またはC99スタイルのイニシャライザ。 CGRect r = {.origin = CGZeroRect、.size = rect.size}; ' –

+0

アイデアありがとう、私はCGRectMakeを使ってみましょう=)... C99スタイルのイニシャライザは素晴らしいですが、十分ではありません。私:P – Fantattitude

答えて

3

my answer~this questionには、UIView内に光沢勾配を描画するためのコードが用意されています。色と描画位置を変更して、必要な線形グラデーションを形成することができます。

1

勾配を作成するためにCGGradientCreateWithColorComponentsCGGradientCreateWithColorsを使用してください。 (後者はCGColorオブジェクトを取ります)次に、CGContextDrawLinearGradientまたはCGContextDrawRadialGradientを使用して描画します。

直線勾配は、勾配の線に垂直な少なくとも2つの方向に無限に広がります。放射状の勾配はあらゆる方向に無限に延びています。グラデーションがビュー外に流出しないようにするには、おそらく、CGContextClipToRectを使用して、ビューの境界をクリッピングパスに追加する必要があります。

3

これは.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 
+0

- (void)getRGBA:メソッドは、UIColorカテゴリにあるはずですね。 – singingAtom

関連する問題