2012-08-26 63 views
0

嗨,下面的代码使一个灰色的小矩形与一个黑暗的边框。角落收音机不工作,我不明白为什么,我试过应用maskToBounds = true,但这只是隐藏整个对象...CAShapeLayer cornerRadius不工作在UIBezierPath

我该如何实现这一目标?由于

CGRect r = CGRectMake(conX, conY, 220, 50); 
    UIBezierPath* conPath = [UIBezierPath bezierPathWithRect:r]; 
    CAShapeLayer* conLayer = [CAShapeLayer layer]; 
    conLayer.path = conPath.CGPath; 
    conLayer.cornerRadius = 5.0; 
    UIColor *bg = [UIColor colorWithWhite:1 alpha:0.7]; 
    [conLayer setFillColor:bg.CGColor]; 
    [conLayer setStrokeColor:[UIColor grayColor].CGColor]; 
    [[self layer] addSublayer:conLayer]; 

回答

6

UPDATE:

您可以简单地使用UIBezierPath(roundedRect: CGRect, cornerRadius: CGFloat).CGPath来创建圆矩形路径


我建议采用圆矩形路径,而不是:

CGRect r = { .size = { 220.0f, 50.0f } } ; 

CAShapeLayer * layer = [ CAShapeLayer layer ] ; 
layer.path = CGPathCreateRoundRect(r, 5.0f) ; 

其中CGPathCreateRoundRect()

CGPathRef CGPathCreateRoundRect(const CGRect r, const CGFloat cornerRadius) 
{ 
    CGMutablePathRef p = CGPathCreateMutable() ; 

    CGPathMoveToPoint(p, NULL, r.origin.x + cornerRadius, r.origin.y) ; 

    CGFloat maxX = CGRectGetMaxX(r) ; 
    CGFloat maxY = CGRectGetMaxY(r) ; 

    CGPathAddArcToPoint(p, NULL, maxX, r.origin.y, maxX, r.origin.y + cornerRadius, cornerRadius) ; 
    CGPathAddArcToPoint(p, NULL, maxX, maxY, maxX - cornerRadius, maxY, cornerRadius) ; 

    CGPathAddArcToPoint(p, NULL, r.origin.x, maxY, r.origin.x, maxY - cornerRadius, cornerRadius) ; 
    CGPathAddArcToPoint(p, NULL, r.origin.x, r.origin.y, r.origin.x + cornerRadius, r.origin.y, cornerRadius) ; 

    return p ; 
} 
+0

如果它是你想要的遮罩效果,请将图层的__mask__属性设置为具有圆形矩形路径的形状图层 – nielsbot

+0

Thanks Niels thats awesome mate! :d) – Baconbeastnz

相关问题