2009-12-14 152 views
6

我还没有真正用Core Graphics做过太多的编程。我倾向于坚持使用QuartzCore,现在它通过图层属性完成了我需要的大量功能:)渐变色圆形矩形

但是,我有一个UIView,它是当前渐变的。我想圆角添加到此的UIView和层属性不这样做,当我画的梯度:

- (void)drawRect:(CGRect)rect { 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    CGGradientRef glossGradient; 
    CGColorSpaceRef rgbColorspace; 
    size_t num_locations = 2; 
    CGFloat locations[2] = { 0.0, 1.0 }; 
    CGFloat components[8] = { 1.0, 1.0, 1.0, 0.95, // Start color 
          1.0, 1.0, 1.0, 0.60 }; // End color 

    rgbColorspace = CGColorSpaceCreateDeviceRGB(); 
    glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); 

    CGRect currentBounds = self.bounds; 
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f); 
    CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds)); 
    CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0); 

    CGGradientRelease(glossGradient); 
    CGColorSpaceRelease(rgbColorspace); 
} 

我真的不知道,我应该在drawRect方法进行四舍五入。谢谢。

+0

我已经更新了以下工作示例我的答案。请让我知道这是否适合你。 – nash 2009-12-17 18:14:14

回答

7

您是否检查了上一个问题发布?我读了一段关于屏蔽UIViews的内容。我认为同样适用于几乎在其上使用的drawRect

How to mask a square image into an image with round corners in the iPhone SDK?


这里就是我所做的一切物体,并能正常工作,据我可以告诉。

首先,我借用了上述帖子中的NilObject先生的代码片段。

我修改它适合在一个对象(因为他写它作为一个C函数,而不是方法)

我UIView的子类来创建自己的自定义视图。我重载initWithRect:使我的背景透明。

所以基本上:

  • 设置透明背景(在INIT),或削波将uggly
  • 在drawRect中,第一夹子
  • ,然后绘制裁剪区域
内部

以下是一个工作示例:

// 
// TeleView.m 
// 

#import "TeleView.h" 


@implementation TeleView 
/**** in init methods, set background to transparent, 
     otherwise, clipping shows a black background ****/ 

- (id) initWithFrame:(CGRect)frame { 
    if((self = [super initWithFrame:frame])) { 
     [self setBackgroundColor:[UIColor colorWithWhite:0.0f alpha:0.0f]]; 
    } 
    return self; 
} 
- (void) clipCornersToOvalWidth:(float)ovalWidth height:(float)ovalHeight 
{ 
    float fw, fh; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGRect rect = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height); 

    if (ovalWidth == 0 || ovalHeight == 0) { 
     CGContextAddRect(context, rect); 
     return; 
    } 
    CGContextSaveGState(context); 
    CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect)); 
    CGContextScaleCTM (context, ovalWidth, ovalHeight); 
    fw = CGRectGetWidth (rect)/ovalWidth; 
    fh = CGRectGetHeight (rect)/ovalHeight; 
    CGContextMoveToPoint(context, fw, fh/2); 
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); 
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); 
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); 
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); 
    CGContextClosePath(context); 
    CGContextRestoreGState(context); 
} 

-(void)drawRect:(CGRect)rect { 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    /**** here is what I modified. ****/ 
    [self clipCornersToOvalWidth:20.0f height:20.0f]; 
    CGContextClip(currentContext); 

    /**** below this is your own code ****/ 
    CGGradientRef glossGradient; 
    CGColorSpaceRef rgbColorspace; 
    size_t num_locations = 2; 
    CGFloat locations[2] = { 0.0, 1.0 }; 
    CGFloat components[8] = { 1.0, 0.0, 0.0, 0.60, // Start color 
    0.0, 1.0, 0.0, 0.40 }; // End color 

    rgbColorspace = CGColorSpaceCreateDeviceRGB(); 
    glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); 

    CGRect currentBounds = self.bounds; 
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f); 
    CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds)); 
    CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0); 

    CGGradientRelease(glossGradient); 
    CGColorSpaceRelease(rgbColorspace); 

} 

@end 
+0

是的,我以前见过这个。我只是不能将它与我自己的代码合并:( – runmad 2009-12-15 03:24:12

+0

当你尝试然后会发生什么?你看到了什么?任何错误?你是如何尝试集成ir? – nash 2009-12-15 06:06:56

+0

它是或者..如果我绕过角落,我不能使用渐变代码,反之亦然,我相信我正在合并两个不正确的,但CG是不是真的我有很多经验 – runmad 2009-12-15 23:29:19

7
NSArray *colors = [NSArray arrayWithObjects:fromColor.CGColor,toColor.CGColor, nil]; 

NSNumber *stopOne = [NSNumber numberWithFloat:0.0]; 
NSNumber *stopTwo = [NSNumber numberWithFloat:1.0]; 

NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, nil]; 

CAGradientLayer *headerLayer = [CAGradientLayer layer]; 
headerLayer.colors = colors; 
headerLayer.locations = locations; 

headerLayer.borderColor = borderColor.CGColor; // border line color**strong text** 
headerLayer.borderWidth = width;// For Thickness of border line 
headerLayer.cornerRadius = radius;//For Rounded Corner if You want to make rounded Corner 

headerLayer.frame = frame; 

[view.layer insertSublayer:headerLayer atIndex:0]; 

还要确保导入“QuartzCore”框架