Swift用UIBezierPath来画圆角矩形、自定义多路径图形
最好的特点就是可以自定义路径,设置圆角和描边都很方便,以下为代码和效果,均在playground中实现
1、首先实现一个圆角矩形,并对此路径描边,为其绘制一个轮廓。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//: Playground - noun: a place where people can play import UIKit class MyView : UIView { override func drawRect ( rect : CGRect ) { var pathRect = UIEdgeInsetsInsetRect ( self . bounds , UIEdgeInsetsMake ( 1 , 1 , 1 , 1 )) var path = UIBezierPath ( roundedRect : pathRect , cornerRadius : 10 ) path . lineWidth = 4 UIColor . greenColor (). setFill () UIColor . blackColor (). setStroke () path . fill () path . stroke () } } let viewRect = CGRect ( x : 0 , y : 0 , width : 100 , height : 100 ) let myEmptyView = MyView ( frame : viewRect ) |
tips:所有绘制操作都是按照调用顺序进行的。在本段代码中,我在填充矩形后再对其进行描边。如果交换对path.fill()和path.stroke()的调用顺序,将会得到一个稍有不同的结果,绿色填充将会略微覆盖黑色描边,效果图如下。
2、下面自定义一条路径,确定几个点,然后像画笔一样连线!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
//: Playground - noun: a place where people can play import UIKit class MyView : UIView { override func drawRect ( rect : CGRect ) { var bezierPath = UIBezierPath () //创建一个矩形,它的所有边都内缩5% var drawingRect = CGRectInset ( self . bounds , self . bounds . size . width * 0.05 , self . bounds . size . height * 0.05 ) //确定组成绘画的点 var topLeft = CGPointMake ( CGRectGetMinX ( drawingRect ), CGRectGetMinY ( drawingRect )) var topRight = CGPointMake ( CGRectGetMaxX ( drawingRect ), CGRectGetMinY ( drawingRect )) var bottomLeft = CGPointMake ( CGRectGetMinX ( drawingRect ), CGRectGetMaxY ( drawingRect )) var bottomRight = CGPointMake ( CGRectGetMaxX ( drawingRect ), CGRectGetMaxY ( drawingRect )) var center = CGPointMake ( CGRectGetMidX ( drawingRect ), CGRectGetMinY ( drawingRect )) //开始绘制 bezierPath . moveToPoint ( topLeft ) bezierPath . addLineToPoint ( topRight ) bezierPath . addLineToPoint ( bottomLeft ) bezierPath . addCurveToPoint ( bottomRight , controlPoint1 : center , controlPoint2 : center ) //使路径闭合,结束绘制 bezierPath . closePath () //设定颜色,并绘制它们 UIColor . redColor (). setFill () UIColor . blackColor (). setStroke () bezierPath . fill () bezierPath . stroke () } } let viewRect = CGRect ( x : 0 , y : 0 , width : 100 , height : 100 ) let myEmptyView = MyView ( frame : viewRect ) |
3、多条子路径也可以。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
//: Playground - noun: a place where people can play import UIKit class MyView : UIView { override func drawRect ( rect : CGRect ) { //创建一条空Bezier路径 let bezierPath = UIBezierPath () //为两个组成部分定义矩形 let squareRect = CGRectInset ( rect , rect . size . width * 0.45 , rect . size . height * 0.05 ) let circleRect = CGRectInset ( rect , rect . size . width * 0.3 , rect . size . height * 0.3 ) let cornerRadius : CGFloat = 20 //创建路径 let circlePath = UIBezierPath ( ovalInRect : circleRect ) let squarePath = UIBezierPath ( roundedRect : squareRect , cornerRadius : cornerRadius ) //将它们添加到主路径 squarePath . appendPath ( circlePath ) bezierPath . appendPath ( squarePath ) //设定颜色并绘制它们 UIColor . redColor (). setFill () //绘制路径 bezierPath . fill () } } let viewRect = CGRect ( x : 0 , y : 0 , width : 100 , height : 100 ) let myEmptyView = MyView ( frame : viewRect ) |
以上就是UIBezierPath的基本用法,用好了将是绘制图形的又一利器。
Swift用UIBezierPath来画圆角矩形、自定义多路径图形的更多相关文章
- nginx-1.12.0版本(编译安装)-自定义安装路径
nginx-1.12.0版本(编译安装)-自定义安装路径 安装路径:/application/nginx-1.12.0 1.前期准备 安装编译需要的gcc和gcc-c++ yum install -y ...
- CKFinder 自定义文件路径扩展ConfigurationPathBuilder
CKFinder 自定义文件路径扩展ConfigurationPathBuilder 打开config.xml当中可以看到如下配置 <basePathBuilderImpl>com.ckf ...
- tomcat 设定自定义图片路径
1.问题 平常图片路径都是在项目目录下存放,都是ip地址+端口号+项目名+图片路径,因为项目需要要把图片从tomcat中分离出来,并且设置可以通过自定义地址访问自定义图片路径. 2.解决 在 tomc ...
- magento 自定义url路径 和 filter data 小结
背景是往一个第三方的搜索插件里面加入filter功能. 首先是路径,插件自己定义了一个router,类似于cms.那首先说说router好了,从入口一路追查的话,会发现最后进入的是Mage_Core_ ...
- Nginx 核心配置-自定义日志路径及清空日志注意事项
Nginx 核心配置-自定义日志路径及清空日志注意事项 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于日志清空注意事项 1>.nginx服务写访问日志是基于acces ...
- Flink FileSink 自定义输出路径——StreamingFileSink、BucketingSink 和 StreamingFileSink简单比较
接上篇:Flink FileSink 自定义输出路径——BucketingSink 上篇使用BucketingSink 实现了自定义输出路径,现在来看看 StreamingFileSink( 据说是S ...
- Flink FileSink 自定义输出路径——BucketingSink
今天看到有小伙伴在问,就想着自己实现一下. 问题: Flink FileSink根据输入数据指定输出位置,比如讲对应日期的数据输出到对应目录 输入数据: 20190716 输出到路径 20190716 ...
- Swift之UIBezierPath
使用UIBezierPath可以创建基于矢量的路径.使用此类可以定义简单的形状,如椭圆.矩形或者有多个直线和曲线段组成的形状等.主要用到的该类的属性包括 moveToPoint: //设置起始点 ad ...
- 使用CAShapeLayer和UIBezierPath画一个自定义半圆弧button
通常我们使用系统自带的UIButton时,一般都是Rect矩形形式的,或则美工给出一张半圆弧的按钮,如图为一张半圆加三角形的按钮,而此时,如果给按钮添加点击事件时,响应事件依然为矩形区域,不符合我们的 ...
随机推荐
- C语言 百人拉百灯问题
题目: 有100人,编号从1到100; 另有100盏灯,编号也从 1到100. 现要求每人去拉能被自己编号整除的所有电灯, 例如编号为1者应把所有的灯都拉一遍, 编号为2者应把所有编号为偶数的灯都拉一 ...
- IOS列表实现动态多列
. //图片列表 NSMutableArray *pictureList; //分组列表 NSMutableArray *indexArr; - (UITableViewCell *)tableVie ...
- Java基础 —— CSS
CSS:层叠样式表(Cascading Style Sheets) --> 提高显示功能,定义样式 html提供了div与span,只为了封装文本数据,div为一行数据,span为行内的数据. ...
- MyEclipse 代码自动提示
默认 myeclipse,输入.才会出现代码提示,不能根据单词前缀提示. 按以下设置就很爽了 .abcdefghijklmnopqrstuvwxyz(,
- 1001Sum Problem
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): ...
- 删除qq历史签名
我们在设置新的个性签名的时候之前的签名会被记录,我们可以用手机qq删除这些历史签名,告别过去,做崭新的自己. 到需要删除的历史签名, 从右至左滑动屏幕
- Swift项目兼容Objective-C问题汇总
Swift项目兼容Objective-C问题汇总 转载自 http://www.cocoachina.com/swift/20150608/12025.html 本文是投稿文章,作者:一叶(博客)欢迎 ...
- 如何防止ASP.NET网站遭受CSRF的攻击
转载地址: http://www.cnblogs.com/shanyou/p/5038794.html?hmsr=toutiao.io&utm_medium=toutiao.io&ut ...
- LDA(Latent Dirichlet Allocation)
转自:http://leyew.blog.51cto.com/5043877/860255#559183-tsina-1-46862-ed0973a0c870156ed15f06a6573c8bf0 ...
- 多线程下载网络歌曲&播放歌曲&并用seekbar调节进度&显示歌曲两边的时间
这里先给一个处理时间格式的代码: /** * 时间的处理 * * @param time * @return */ public static String getTimeFromInt(int t ...