圆角矩形,渐变色

时间:2009-12-14 20:43:34

标签: iphone core-graphics gradient rounded-corners

我没有用Core Graphics做过多少编程。而且我倾向于坚持使用QuartzCore,因为它通过图层属性完成了我需要的很多东西:)

但是,我有一个UIView,它目前是渐变的。我想为这个UIView添加圆角,当我绘制渐变时,layer属性不会这样做:

- (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方法中进行舍入。感谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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”框架

相关问题