貝塞爾曲線是一個畫圖的類,需在drawRect方法中繪制;可以繪制直線、矩形、圓、橢圓以及其他復(fù)雜的圖形
貝塞爾曲線(UIBezierPath)的使用如下:
類方法
//矩形路徑
public convenience init(rect: CGRect)
//橢圓,當rect為正方形是則為圓
public convenience init(ovalIn rect: CGRect)
//圓角矩形、圓路徑
public convenience init(roundedRect rect: CGRect, cornerRadius: CGFloat) // rounds all corners with the same horizontal and vertical radius
//指定矩形的角為圓角
public convenience init(roundedRect rect: CGRect, byRoundingCorners corners: UIRectCorner, cornerRadii: CGSize)
//弧線路徑/扇形
public convenience init(arcCenter center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool)
//CGpath創(chuàng)建
public convenience init(cgPath CGPath: CGPath)
對象方法
public init()
public init?(coder: NSCoder)
屬性
//返回一個不可變的CGPathRef,在UIBezierPath被進一步修改之前有效
open var cgPath: CGPath
//判斷是否為空 只讀屬性
open var isEmpty: Bool { get }
//整個路徑的位置
open var bounds: CGRect { get }
//當前路徑的位置
open var currentPoint: CGPoint { get }
// 線寬
open var lineWidth: CGFloat
//線的末端的類型
open var lineCapStyle: CGLineCap
public enum CGLineCap : Int32 {
case butt = 0 //無端點
case round = 1 //圓形端點
case square = 2 //方形端點
}
//相交線的交點類型
open var lineJoinStyle: CGLineJoin
public enum CGLineJoin : Int32 {
case miter = 0 //直接連接
case round = 1 //圓角連接
case bevel = 2 //斜角連接
}
//miterLimit 屬性設(shè)置或返回最大斜接長度,只有當 lineJoinStyle 屬性為 "kCGLineJoinMiter" 時,miterLimit 才有效
open var miterLimit: CGFloat // Used when lineJoinStyle is kCGLineJoinMiter
//線的精細程度
open var flatness: CGFloat
//填充區(qū)域的方式
open var usesEvenOddFillRule: Bool // Default is NO. When YES, the even-odd fill rule is used for drawing, clipping, and hit testing.
方法
//設(shè)置畫筆的起點位置
open func move(to point: CGPoint)
//從前一個點到添加點的直線
open func addLine(to point: CGPoint)
//繪制曲線(三階貝塞爾曲線) 從起點到結(jié)束點的曲線,有兩個控制點坐標controlPoint1、controlPoint2
open func addCurve(to endPoint: CGPoint, controlPoint1: CGPoint, controlPoint2: CGPoint)
////繪制曲線(二階貝塞爾曲線)從起點到終點的曲線,一個控制點
open func addQuadCurve(to endPoint: CGPoint, controlPoint: CGPoint)
@available(iOS 4.0, *)
//繪制弧線 中線點、半徑、開始角、結(jié)束角、繪制方向
open func addArc(withCenter center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool)
//設(shè)置為封閉的路徑
open func close()
//移除所有的點
open func removeAllPoints()
// Appending paths 兩個貝塞爾曲線路徑拼接
open func append(_ bezierPath: UIBezierPath)// Modified paths@available(iOS 6.0, *)
//創(chuàng)建并返回一個與當前路徑相反的新的貝塞爾曲線
open func reversing() -> UIBezierPath
// 用CGAffineTransform變換路徑的所有點
open func apply(_ transform: CGAffineTransform)
//是否包含指定的點
open func contains(_ point: CGPoint) -> Bool
//設(shè)置虛線
open func setLineDash(_ pattern: UnsafePointer<CGFloat>?, count: Int, phase: CGFloat)
//獲取虛線
open func getLineDash(_ pattern: UnsafeMutablePointer<CGFloat>?, count: UnsafeMutablePointer<Int>?, phase: UnsafeMutablePointer<CGFloat>?)
//填充
open func fill()
//繪制路徑
open func stroke()
//描邊的混合模式
open func fill(with blendMode: CGBlendMode, alpha: CGFloat)
//填充的混合模式
open func stroke(with blendMode: CGBlendMode, alpha: CGFloat)
//修改當前圖形上下文的繪制的可見區(qū)域,之后繪圖操作顯示的內(nèi)容只有發(fā)生在指定路徑的填充區(qū)域
open func addClip()
使用
1、直線
//修改線的顏色
let color = UIColor.cyan;
color.setStroke()
//創(chuàng)建UIBezierPath曲線
let path = UIBezierPath.init();
path.move(to: CGPoint(x: 100, y: 100));
path.addLine(to: CGPoint(x: 200, y: 120));
path.lineWidth = 2.0;
path.stroke();
line.png
2、矩形
let color = UIColor.cyan;
color.set();
let path = UIBezierPath.init(rect: CGRect(x: 100, y: 100, width: 150, height: 100));
path.lineWidth = 2.0;
path.stroke();
rect.png
3、圓/橢圓 當長和寬相等時則為圓;當長和寬不等時為橢圓
let color = UIColor.cyan;
color.set();
let path = UIBezierPath.init(ovalIn: CGRect(x: 100, y: 100, width: 130, height: 100));
path.lineWidth = 2.0;
path.fill();//區(qū)域內(nèi)填充
path.stroke();
oval.png
4、圓/圓角矩形
a、半徑大于或等于正方形的邊長一半時為圓,此時半徑不起作用(此處描述有問題)
b、半徑小于于正方形的邊長一半時為圓角矩形
c、當圖形長和寬不等時為圓角矩形,如圖"Rounded_R"
let color = UIColor.cyan;
color.setFill();
let path = UIBezierPath.init(roundedRect: CGRect(x: 100, y: 100, width: 100, height: 100), cornerRadius: 33);
path.lineWidth = 2.0;
path.fill();//區(qū)域內(nèi)填充
path.stroke();
問題:a的描述,當為正方形且半徑時邊長的一半是時圓。
當初始化的時候設(shè)置半徑為32時,此時是一個圓角矩形。
當初始化的時候設(shè)置半徑為33時,此時是一個圓。
希望知道的小伙伴解答一下,感激不盡!!!
rect.png
5、圓角矩形:指定矩形的某些角為圓角
let color = UIColor.cyan;
color.setStroke();
let path = UIBezierPath.init(roundedRect: CGRect(x: 100, y: 100, width: 150, height: 100), byRoundingCorners: UIRectCorner(rawValue: (UIRectCorner.topRight.rawValue | UIRectCorner.bottomLeft.rawValue)) ,cornerRadii: CGSize(width: 20, height: 20));
path.lineWidth = 2.0;
path.stroke();
rect.png
6、弧線/扇形
clockwise:方向
let color = UIColor.cyan;
color.set();
let path = UIBezierPath.init(arcCenter: CGPoint(x: 100, y: 100), radius: 20, startAngle: (CGFloat)(45*M_PI/180), endAngle: (CGFloat)(200*M_PI/180), clockwise: false)
path.lineWidth = 2.0;
path.close()
path.stroke();
UIBezierPath.init(arcCenter: CGPoint(x: 100, y: 100), radius: 20, startAngle: (CGFloat)(45*M_PI/180), endAngle: (CGFloat)(200*M_PI/180), clockwise: false)
false.png
UIBezierPath.init(arcCenter: CGPoint(x: 100, y: 100), radius: 20, startAngle: (CGFloat)(45*M_PI/180), endAngle: (CGFloat)(200*M_PI/180), clockwise: true)
true.png
7、繪制貝塞爾曲線三階;controlPoint1:控制點1,controlPoint2:控制點2
let path = UIBezierPath.init()
path.move(to: CGPoint(x: 100, y: 100))
path.addCurve(to: CGPoint(x: 200, y: 100), controlPoint1: CGPoint(x: 130, y: 60), controlPoint2: CGPoint(x: 180, y: 130))
path.lineWidth = 2.0;
path.stroke();
c.png
8、繪制貝塞爾曲線二階;controlPoint:控制點
let path = UIBezierPath.init()
path.move(to: CGPoint(x: 100, y: 100))
path.addQuadCurve(to: CGPoint(x: 200, y: 100), controlPoint: CGPoint(x: 150, y: 60))
path.lineWidth = 2.0;
path.stroke();
c.png