//繪制漸變色
? funcdrawGradients(colors: [Any], locations: [NSNumber]? = [0,1], isHorizontal:Bool? =true) {
? ? letgradientLayer =CAGradientLayer()
? ? gradientLayer.frame=self.bounds
? ? gradientLayer.colors= colors
? ? gradientLayer.locations= locations
? ? ifisHorizontal ??true{
? ? ? gradientLayer.startPoint=CGPoint(x:0, y:0)
? ? ? gradientLayer.endPoint=CGPoint(x:1, y:0)
? ? }else{
? ? ? gradientLayer.startPoint=CGPoint(x:0, y:0)
? ? ? gradientLayer.endPoint=CGPoint(x:0, y:1)
? ? }
? ? removeCAShapeLayerAndCAGradientLayer(layer: self.layer)
? ? self.layer.insertSublayer(gradientLayer, at:0)
? }
? //繪制虛線
? funcdrawLineOfDash(lineLength:Int, lineSpacing:Int, lineColor:UIColor) {
? ? letpath =CGMutablePath()
? ? path.move(to:CGPoint(x:0, y:0), transform: .identity)
? ? path.addLine(to:CGPoint(x:self.frame.size.width, y:0), transform: .identity)
? ? letshapeLayer =CAShapeLayer()
? ? shapeLayer.bounds=self.bounds
? ? shapeLayer.position=CGPoint(x:self.frame.width/2, y:self.frame.height)
? ? shapeLayer.fillColor = UIColor.clear.cgColor
? ? shapeLayer.strokeColor= lineColor.cgColor
? ? shapeLayer.lineWidth = self.frame.size.height
? ? shapeLayer.lineJoin= .round
? ? shapeLayer.lineDashPattern= [NSNumber(integerLiteral: lineLength),NSNumber(integerLiteral: lineSpacing)]
? ? shapeLayer.path= path
? ? removeCAShapeLayerAndCAGradientLayer(layer: self.layer)
? ? self.layer.addSublayer(shapeLayer)
? }
? //繪制漸變色的虛線
? funcdrawLineOfDashWithGradients(lineLength:Int, lineSpacing:Int, lineColor:UIColor, colors: [Any], locations: [NSNumber]? = [0,1], isHorizontal:Bool? =true) {
? ? letpath =CGMutablePath()
? ? path.move(to:CGPoint(x:0, y:0), transform: .identity)
? ? path.addLine(to:CGPoint(x:self.frame.size.width, y:0), transform: .identity)
? ? letshapeLayer =CAShapeLayer()
? ? shapeLayer.bounds=self.bounds
? ? shapeLayer.position=CGPoint(x:self.frame.width/2, y:self.frame.height)
? ? shapeLayer.fillColor = UIColor.clear.cgColor
? ? shapeLayer.strokeColor= lineColor.cgColor
? ? shapeLayer.lineWidth = self.frame.size.height
? ? shapeLayer.lineJoin= .round
? ? shapeLayer.lineDashPattern= [NSNumber(integerLiteral: lineLength),NSNumber(integerLiteral: lineSpacing)]
? ? shapeLayer.path= path
? ? letgradientLayer =CAGradientLayer()
? ? gradientLayer.frame=self.bounds
? ? gradientLayer.colors= colors
? ? gradientLayer.locations= locations
? ? ifisHorizontal ??true{
? ? ? gradientLayer.startPoint=CGPoint(x:0, y:0)
? ? ? gradientLayer.endPoint=CGPoint(x:1, y:0)
? ? }else{
? ? ? gradientLayer.startPoint=CGPoint(x:0, y:0)
? ? ? gradientLayer.endPoint=CGPoint(x:0, y:1)
? ? }
? ? gradientLayer.mask?.sublayers?.removeAll()
? ? gradientLayer.mask= shapeLayer
? ? removeCAShapeLayerAndCAGradientLayer(layer: self.layer)
? ? self.layer.addSublayer(gradientLayer)
? }
? func removeCAShapeLayerAndCAGradientLayer(layer: CALayer) {
? ? layer.sublayers?.removeAll(where: { (sublayer) ->Boolin
? ? ? ifsublayerisCAGradientLayer||sublayerisCAShapeLayer{
? ? ? ? return true
? ? ? }
? ? ? return false
? ? })
? }