Swift-畫虛線、虛線圓
阿新 • • 發佈:2019-01-22
程式碼如下:
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, nil, 20, 100)
CGPathAddLineToPoint(mdotteShapePath, nil, 20, 200)
CGPathAddLineToPoint(mdotteShapePath, nil, 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
效果如下:
虛線圓
虛直線