[iOS - (void)drawRect:(CGRect)rect] 绘制一个居中的矩形

使用到:
CGRectInset
CGRectoffset
UIEdgeInsetsInsetRect
这三个函数的使用情况

//CGRectInset  将原来的矩形放大或者缩小,正表示缩小,-表示放大。
CGRect rect= CGRectMake(20, 50, 100, 80);
 CGRect rect1=CGRectInset(rect, -10, 20);
 NSLog(@"%@",p(rect1));
//输出结果:2014-11-22 18:48:55.351 TestCGRectInset[8893:60b] {{10, 70}, {120, 40}}
//CGRectOffset 这个函数就是将原来矩形的坐标点变化一下,就是左上角点
CGRect rect= CGRectMake(20, 50, 100, 80);
CGRect rect1=CGRectOffset(rect, -10, 20);
NSLog(@"%@",p(rect1));
//输出结果:2014-11-22 18:51:58.217 TestCGRectInset[8913:60b] {{10, 70}, {100, 80}}
 //UIEdgeInsetsInsetRect 表示在原来的rect基础上根据边缘距离内切一个rect出来
CGRect rect= CGRectMake(20, 50, 100, 80);
UIEdgeInsets ed=UIEdgeInsetsMake(-3, -4, -5, -6);
CGRect  r=  UIEdgeInsetsInsetRect(rect, ed);
NSLog(@"%@",p(r));

绘制代码如下:

- (void)drawRect:(CGRect)rect
{
//    rect.size.height = 140;
//    rect.size.width  = 180;
//    rect请设置为屏幕宽高

    const CGFloat RECT_PADDING = 8.0;
    rect = CGRectInset(rect, (rect.size.width-180)/2, (rect.size.height-140)/2);

    const CGFloat ROUND_RECT_CORNER_RADIUS = 5.0;
    CGPathRef roundRectPath = NewPathWithRoundRect(rect, ROUND_RECT_CORNER_RADIUS);

    CGContextRef context = UIGraphicsGetCurrentContext();

    const CGFloat BACKGROUND_OPACITY = 0.85;
    CGContextSetRGBFillColor(context, 0, 0, 0, BACKGROUND_OPACITY);
    CGContextAddPath(context, roundRectPath);
    CGContextFillPath(context);

    const CGFloat STROKE_OPACITY = 0.25;
    CGContextSetRGBStrokeColor(context, 1, 1, 1, STROKE_OPACITY);
    CGContextAddPath(context, roundRectPath);
    CGContextStrokePath(context);

    CGPathRelease(roundRectPath);
}

CGPathRef NewPathWithRoundRect(CGRect rect, CGFloat cornerRadius)
{
    //
    // Create the boundary path
    //
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL,
                      rect.origin.x,
                      rect.origin.y + rect.size.height - cornerRadius);

    // Top left corner
    CGPathAddArcToPoint(path, NULL,
                        rect.origin.x,
                        rect.origin.y,
                        rect.origin.x + rect.size.width,
                        rect.origin.y,
                        cornerRadius);

    // Top right corner
    CGPathAddArcToPoint(path, NULL,
                        rect.origin.x + rect.size.width,
                        rect.origin.y,
                        rect.origin.x + rect.size.width,
                        rect.origin.y + rect.size.height,
                        cornerRadius);

    // Bottom right corner
    CGPathAddArcToPoint(path, NULL,
                        rect.origin.x + rect.size.width,
                        rect.origin.y + rect.size.height,
                        rect.origin.x,
                        rect.origin.y + rect.size.height,
                        cornerRadius);

    // Bottom left corner
    CGPathAddArcToPoint(path, NULL,
                        rect.origin.x,
                        rect.origin.y + rect.size.height,
                        rect.origin.x,
                        rect.origin.y,
                        cornerRadius);

    // Close the path at the rounded rect
    CGPathCloseSubpath(path);

    return path;
}

如图:
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值