美文网首页
swift 画圆形进度条

swift 画圆形进度条

作者: 有理想有暴富的小青年 | 来源:发表于2018-04-11 10:41 被阅读140次

    新建一个继承自uiview的类   FCCycleView是我的类名 oc的UIview   swift不写init方法会报错 写了又多此一举 还不能写不带参数的 init函数  但是可以写便利构造函数(but 会crash) 所以就参数问题写成了属性  参考了其他的文章 几乎都是写成带frame的函数 (有大神知道的话 忘告知 谢谢!!!)

     import UIKit

    class FCCycleView:UIView{

        varprogressWidth :CGFloat=10//线宽

        varbottomColor :UIColor?//底线

        vartopColor :UIColor?//progress线条

        var origin :CGPoint=CGPoint(x:0, y:0)//圆点 

        var radius :CGFloat=0//半径

        var startAngle :CGFloat=0//起始

        var endAngle :CGFloat=0//结束

        var blocks : ((_progressfloat:CGFloat)->())?//回调进度比例的

        //进度label

        private lazy var progressLabel :UILabel= {

            let lab =UILabel()

            lab.frame=CGRect(x:0, y:0, width:100, height:15)

            lab.center = CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2)

            lab.textColor = UIColor.red

            lab.font=UIFont.systemFont(ofSize:13)

            lab.textAlignment= .center

            returnlab

        }()

        privatelazyvarbottomLayer :CAShapeLayer= {

            letlayers =CAShapeLayer()

            layers.fillColor=UIColor.clear.cgColor

            returnlayers

        }()

        privatelazyvartopLayer :CAShapeLayer= {

            letlayers =CAShapeLayer()

            layers.lineCap=kCALineCapRound

            layers.fillColor=UIColor.clear.cgColor

            returnlayers

        }()

        overridefuncdraw(_rect:CGRect) {

            setUPUI()

            addRound()

            setMethoud()

            blocks= {(profloat:CGFloat)->()in

                self.progressLabel.text="\(Int(profloat *100))"+"%"

                self.startAngle= -CGFloat(Double.pi/2)

                self.endAngle=self.startAngle+ profloat *CGFloat(Double.pi*2)

                lettopPath  =UIBezierPath.init(arcCenter:self.origin, radius:self.radius, startAngle:self.startAngle, endAngle:self.endAngle, clockwise:true)

                self.topLayer.path=topPath.cgPath//添加路径

                //添加动画

                letpathAnimation =CABasicAnimation.init(keyPath:"strokeEnd")

                pathAnimation.duration=1//动画持续时间

                pathAnimation.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseOut)

                pathAnimation.fromValue=0

                pathAnimation.toValue=1

                self.topLayer.add(pathAnimation, forKey:"strokeEndAnimation")

            }

        }

        overrideinit(frame:CGRect) {

            super.init(frame: frame)

            self.backgroundColor=UIColor.white

        }

        requiredinit?(coder aDecoder:NSCoder) {

            fatalError("init(coder:) has not been implemented")

        }

        funcsetUPUI() {

            layer.addSublayer(bottomLayer)

            layer.addSublayer(topLayer)

            addSubview(progressLabel)

        }

        funcaddRound() {

            origin = CGPoint(x: self.bounds.size.width/2, y: self.bounds.size.height/2)

            radius = self.bounds.size.width/2

            letbottomPath  =UIBezierPath.init(arcCenter:origin, radius:radius, startAngle:0, endAngle:CGFloat(Double.pi*2), clockwise:true)

            bottomLayer.path=bottomPath.cgPath

        }

        funcsetMethoud()  {

            bottomLayer.strokeColor = bottomColor?.cgColor

            topLayer.strokeColor = topColor?.cgColor

            topLayer.lineWidth = progressWidth

            bottomLayer.lineWidth=progressWidth

        }

    }

    VC调用方法


    我用的snapit 约束 (可以打开下面注释)

    import UIKit

    classTargetCycleVC:UIViewController{

    //    画圆

        privatelazyvarcircleProgress :FCCycleView= {

            letcycle =FCCycleView()

            cycle.frame=CGRect(x:100, y:100, width:200, height:200)

            cycle.topColor=UIColor.purple

            cycle.bottomColor=UIColor.cyan

            cycle.progressWidth=10

            returncycle

        }()

        overridefuncviewDidLoad() {

            super.viewDidLoad()

            view.backgroundColor=UIColor.white

            setUPUI()

    //        addLayOuts()

        }

        overridefunctouchesBegan(_touches:Set, with event:UIEvent?) {

    //        回调圆的比例 值为0-1

            if self.circleProgress.blocks != nil{

                self.circleProgress.blocks!(0.88)

            }

        }

        privatefuncsetUPUI() {

            view.addSubview(circleProgress)

        }

    //    private func addLayOuts() {

    //        circleProgress.snp.makeConstraints { (make) in

    //            make.top.equalTo(view.snp.top).offset(50)

    //            make.centerX.equalTo(view.snp.centerX)

    //            make.size.equalTo(CGSize(width: 200, height:200))

    //        }

    //    }

    }

    源码地址 麻烦给个star: https://github.com/fc931127/cycle.git

    相关文章

      网友评论

          本文标题:swift 画圆形进度条

          本文链接:https://www.haomeiwen.com/subject/vjkahftx.html