0

大家好,下面的代码制作了一个带有深色边框的灰色小矩形。角落收音机不工作,我不知道为什么,我尝试应用 maskToBounds = true,但这只是隐藏了整个对象......

我怎样才能做到这一点?谢谢

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];
4

1 回答 1

6

更新:

您可以简单地使用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 ;
}
于 2012-08-26T23:25:16.307 回答