Swift-画虚线、虚线圆

代码如下:

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

效果如下:
虚线圆
Swift-画虚线、虚线圆_第1张图片

虚直线
Swift-画虚线、虚线圆_第2张图片

你可能感兴趣的:(iOS基础(Swift))