Core Animation - 如何来绘制三个圆角一个直角的矩形

216 阅读1分钟

跟绘制火柴人方法类似,但这里我们不需要一条条的线来绘制,有更快捷的方法:

//指定矩形大小
  CGRect rect=CGRectMake(50, 300, 100, 100);
  //设置圆角半径
    CGSize size=CGSizeMake(20, 20);
    //拿出需要设置改动的角
    UIRectCorner corners=UIRectCornerTopRight | UIRectCornerBottomRight |UIRectCornerBottomLeft;
     //这里是上面两步的综合,具体的操作执行
    UIBezierPath *rectPath=[UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:size];
    //图形子类来渲染
    CAShapeLayer *rectLayer=[CAShapeLayer layer];
    //线条颜色
    rectLayer.strokeColor=[UIColor greenColor].CGColor;
    //填充颜色
    rectLayer.fillColor=[UIColor orangeColor].CGColor;
    //线条宽度
    rectLayer.lineWidth=5;
    //起始结束点的样式
    rectLayer.lineJoin=kCALineJoinRound;
    //线条拐角的样式
    rectLayer.lineCap=kCALineCapRound;
    rectLayer.path=rectPath.CGPath;
    [self.view.layer addSublayer:rectLayer];

代码参见火柴人代码下面的代码:github.com/codeliu6572…