class CircleView: UIView {
? ? override?func?draw(_rect:CGRect) {
? ? ? //1.添加設(shè)計師提供一張漸變色圖
? ? ? ? let?imageView =UIImageView(image:UIImage.init(named:"gradient_image"))
? ? ? ? imageView.frame=self.bounds
? ? ? ? self.addSubview(imageView)
? ? ? //2.畫貝塞爾曲線蛤奢,例如畫弧形
? ? ? ? let path = UIBezierPath(arcCenter: CGPoint(x: self.bounds.size.width * 0.5, y: self.bounds.size.height * 0.5),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? radius:UIScreen.main.bounds.size.width*0.17,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? startAngle:0.0,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? endAngle:CGFloat(2.0* .pi),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? clockwise:true)
? ? ? ? path.lineWidth=60
? ? ? //3.新建CAShapeLayer
? ? ? ??let?shapeLayer =CAShapeLayer()
? ? ? ? shapeLayer.frame=self.bounds
? ? ? ? shapeLayer.position=CGPoint(x:self.bounds.size.width*0.5,y:self.bounds.size.height*0.5)
? ? ? ? shapeLayer.path= path.cgPath
? ? ? ? shapeLayer.fillColor=UIColor.clear.cgColor
? ? ? ? shapeLayer.strokeColor=UIColor.white.cgColor
? ? ? ? shapeLayer.lineWidth= path.lineWidth
? ? ? //4.設(shè)置 第1步的圖片的蒙版為第3步的CAShapeLayer
? ? ? ? self.layer.mask= shapeLayer
? ? }
}
和oc版本基本一致秀仲,只是語法略微不同,涉及的相關(guān)圖片見?漸變色餅圖 - OC版