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

news/2024/7/20 23:03:01 标签: ios, 函数, CGRectInse

使用到:
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;
}

如图:
这里写图片描述


http://www.niftyadmin.cn/n/1557349.html

相关文章

linux之 修改磁盘调度算法

IO调度器的总体目标是希望让磁头能够总是往一个方向移动,移动到底了再往反方向走,这恰恰就是现实生活中的电梯模型,所以IO调度器也被叫做电梯. (elevator)而相应的算法也就被叫做电梯算法.而Linux中IO调度的电梯算法有好几种,一个叫做as(Anticipatory),一个叫做 cfq(Complete F…

[iOS js与oc原生互相调用] js调用oc的两种方式

##先总结 参考帖子: oc/js通信demo:(包括WKWebView/UIWebView). 包括js以及对象方式和回调方式从oc原生获取值 iOS js oc相互调用(JavaScriptCore)(二) (核心js 中是通过一个对象来调用方法的 JSExport ) http://blog.csdn.net…

react报:export useHistory (imported as useHistory) was not found in react-router-dom

报错信息: react-router-dom v6 里 export useHistory (imported as useHistory) was not found in react-router-dom; 解决方案: 从报错 很容易看出来 是 useHistory 引入的问题。这个就涉及 react-router-dom这个插件的版本问题了。 react-router…

[iOS 获取照片后压缩图片] 64*64像素 小于4k

头像压缩 通过base64数据上传 1.通过相机/相册获取图片 用到的代理 interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate> //打开相机相册方法如下: - (void)getImageFromIpc {UIImagePickerController *picker [[UIImage…

Python的SQL性能测试

前言 测试异步与同步下Python对于PostgreSQL数据库操作的性能 通过测试同步以及异步下对于数据库的增加和查找操作&#xff0c;以进行性能评估。更直观的以及量化地感受同步以及异步下的性能差距。 环境初始化 代码地址 需要安装pipenv&#xff0c;详细内容可参考 pip3.6 insta…

html标签定义

## HTML骨架格式 日常生活的书信&#xff0c;我们要遵循共同的约定。 <img src"media/mess.png" alt""> 同理&#xff1a;HTML 有自己的语言语法骨架格式&#xff1a; html<HTML> <head> <title></title> </head> &…

js 计算两个日期间所有的日期(moment.js实现)

使用步骤 1.先引入 moment.js cdn 引入 <script src"https://cdn.bootcss.com/moment.js/2.20.1/moment.min.js"></script> <script src"https://cdn.bootcss.com/moment.js/2.20.1/locale/zh-cn.js"></script> 或者 直接下载…

[iOS 从字典取BOOL值的问题] 返回 false 后 boolValue 为nil问题

先看后端返回的json串 {"payTools":[{"account":{"accAlias":"1000046","accNo":"1127","balance":"","expiry":"","freezedBalance":"","ki…