2013-07-18 50 views
0

我写了一个简单的drawRect,用于绘制圆圈,放下阴影并用蓝色填充。我已经成功地画了一个圆圈并填充为蓝色,但是我的影子没有生效。直到我的drawRect有维[[CustomButton alloc] initWithFrame:CGRectMake(0,0,0)),而不是填充,如果我抚摸我的圆圈,我看到阴影在圆圈内,填充期间它被填充颜色绘制核心图形 - 阴影未见

50,50)];

@implementation CustomButton 

- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) 
{ 
    [self setBackgroundColor:[UIColor clearColor]]; 
} 
return self; 
} 

- (void)drawRect:(CGRect)rect 
{ 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGColorRef colorRef = [[UIColor blueColor] CGColor]; 
CGContextSetStrokeColorWithColor(context, colorRef); 
CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]); 


CGRect buttonRect = CGRectInset(rect, 3, 3); 

CGPoint centerPoint = CGPointMake(CGRectGetMidX(buttonRect ),  CGRectGetMidY(buttonRect)); 
CGFloat radius = CGRectGetWidth(buttonRect)/2; 

CGContextSaveGState(context); 
CGContextSetShadow(context, CGSizeMake(15.0, 20.0), 1.0); 
CGContextAddArc(context, centerPoint.x, centerPoint.y, radius, 0, 2*M_PI, 1); 
CGContextClip(context); 
CGContextFillRect(context, buttonRect); 
CGContextRestoreGState(context); 


CGColorRelease(colorRef); 

} 

@end 

谢谢

回答

0

试试这个:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGColorRef colorRef = [[UIColor blueColor] CGColor]; 
    CGContextSetStrokeColorWithColor(context, colorRef); 
    CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]); 


    CGRect buttonRect = CGRectInset(rect, 3, 3); 

    CGPoint centerPoint = CGPointMake(CGRectGetMidX(buttonRect ),  CGRectGetMidY(buttonRect)); 
    CGFloat radius = CGRectGetWidth(buttonRect)/2; 

    CGContextSaveGState(context); 
    CGContextSetShadow(context, CGSizeMake(2.0, 2.0), 2.0); 
    CGContextAddArc(context, centerPoint.x, centerPoint.y, radius, 0, 2*M_PI, 1); 
    //CGContextClip(context); 
    CGContextFillPath(context); 
    CGContextRestoreGState(context); 


    CGColorRelease(colorRef); 
} 

你的身影呈长方形,这就是为什么它不圆下可见。我将呼叫CGContextFillRect更改为CGContextFillPath,您已使用CGContextAddArc创建的路径。

这是你要去的吗?

编辑

您可以在这里找到一个项目:https://bitbucket.org/reydan/so_circleshadow

+0

它的工作。谢谢。 – slysid