代码如下:
case .LayerType_XuXian_Circle:
self.title = "画虚线"
//画虚线
let dotteShapLayer = CAShapeLayer()
let mdotteShapePath = CGPathCreateMutable()
dotteShapLayer.fillColor = UIColor.clearColor().CGColor
dotteShapLayer.strokeColor = UIColor.orangeColor().CGColor
dotteShapLayer.lineWidth = 2.0
CGPathAddEllipseInRect(mdotteShapePath,nil,CGRectMake(100.0,150.0,200.0,200.0))
dotteShapLayer.path = mdotteShapePath
let arr :NSArray = NSArray(array: [10,5])
dotteShapLayer.lineDashPhase = 1.0
dotteShapLayer.lineDashPattern = arr as? [NSNumber]
view.layer.addSublayer(dotteShapLayer)
break
case .LayerType_XuXian_Line:
self.title = "画虚线直线"
//画虚线
let dotteShapLayer = CAShapeLayer()
let mdotteShapePath = CGPathCreateMutable()
dotteShapLayer.fillColor = UIColor.clearColor().CGColor
dotteShapLayer.strokeColor = UIColor.orangeColor().CGColor
dotteShapLayer.lineWidth = 2.0
CGPathMoveToPoint(mdotteShapePath,20,100)
CGPathAddLineToPoint(mdotteShapePath,200)
CGPathAddLineToPoint(mdotteShapePath,200,200)
dotteShapLayer.path = mdotteShapePath
let arr :NSArray = NSArray(array: [10,5])
dotteShapLayer.lineDashPhase = 1.0
dotteShapLayer.lineDashPattern = arr as? [NSNumber]
view.layer.addSublayer(dotteShapLayer)
break
效果如下:
虚线圆
虚直线