Swift用UIBezierPath来画圆角矩形、自定义多路径图形

更新时间:2023-06-23 08:03:39 阅读: 评论:0

Swift⽤UIBezierPath来画圆⾓矩形、⾃定义多路径图形
最好的特点就是可以⾃定义路径,设置圆⾓和描边都很⽅便,以下为代码和效果,均在playground中实现
1、⾸先实现⼀个圆⾓矩形,并对此路径描边,为其绘制⼀个轮廓。
//: Playground - noun: a place where people can play
previouspageimport UIKit
class MyView : UIView{
override func drawRect(rect: CGRect) {
var pathRect = UIEdgeIntsIntRect(lf.bounds, UIEdgeIntsMake(1, 1, 1, 1))
var path = UIBezierPath(roundedRect: pathRect, cornerRadius: 10)
path.lineWidth = 4
UIColor.blackColor().tStroke()
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()的调⽤顺序,将会得到⼀个稍有不同的结果,绿⾊填充将会略微覆盖⿊⾊描边,效果图如下。
values2、下⾯⾃定义⼀条路径,确定⼏个点,然后像画笔⼀样连线!
post production//: Playground - noun: a place where people can play
import UIKit
class MyView : UIView{
override func drawRect(rect: CGRect) {
var bezierPath = UIBezierPath()
//创建⼀个矩形,它的所有边都内缩5%
var drawingRect = CGRectInt(lf.bounds, lf.bounds.size.width*0.05, lf.bounds.size.height*0.05)
/
obv
/确定组成绘画的点
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.addLineToPoint(topRight)
bezierPath.addLineToPoint(bottomLeft)
bezierPath.addCurveToPoint(bottomRight, controlPoint1: center, controlPoint2: center)
/
/使路径闭合,结束绘制
bezierPath.cloPath()
//设定颜⾊,并绘制它们
UIColor.blackColor().tStroke()
bezierPath.fill()
bezierPath.stroke()
}
}
let viewRect = CGRect(x: 0, y: 0, width: 100, height: 100)
let myEmptyView = MyView(frame:viewRect)
3、多条⼦路径也可以。
//: Playground - noun: a place where people can play
import UIKit
class MyView : UIView{
override func drawRect(rect: CGRect) {
//创建⼀条空Bezier路径
let bezierPath = UIBezierPath()
//为两个组成部分定义矩形
let squareRect = CGRectInt(rect, rect.size.width*0.45, rect.size.height*0.05)
let circleRect = CGRectInt(rect, rect.size.width*0.3, rect.size.height*0.3)supreme中文叫什么
let cornerRadius : CGFloat = 20
loveaffair/
/创建路径
let circlePath = UIBezierPath(ovalInRect: circleRect)
let squarePath = UIBezierPath(roundedRect: squareRect, cornerRadius: cornerRadius)        //将它们添加到主路径
squarePath.appendPath(circlePath)
bezierPath.appendPath(squarePath)
//设定颜⾊并绘制它们
//绘制路径
bezierPath.fill()
}
}
ne
zubulet viewRect = CGRect(x: 0, y: 0, width: 100, height: 100)性别英文
let myEmptyView = MyView(frame:viewRect)
以上就是UIBezierPath的基本⽤法,⽤好了将是绘制图形的⼜⼀利器。

本文发布于:2023-06-23 08:03:39,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/1019644.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:路径   绘制   定义
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图