自定义Progress

来源:互联网 发布:网络菲儿的个人资料 编辑:程序博客网 时间:2024/06/02 15:57

1,创建一个类继承UI View

class ProgressControl: UIView {    required init(coder aDecoder: NSCoder) {        super.init(coder:aDecoder)    }    override init(frame: CGRect) {        super.init(frame: frame)                // 设置背景为白色        self.backgroundColor = UIColor(white: 1, alpha: 0)    }     var indictorLabel:UILabel?        override func drawRect(rect: CGRect)    {        var ctx = UIGraphicsGetCurrentContext()                // 1,以宽度的一半为圆心        var r = rect.width/2                // 2,添加一个标签        if indictorLabel == nil{            indictorLabel = UILabel(frame: CGRect(x: r-20, y: r-20, width: 60, height: 40))            addSubview(indictorLabel!)        }                var fe = getProgressValue() * 100        indictorLabel?.text = "\(fe)%"                // 3,添加一个圆形背景        CGContextAddArc(ctx, r, r, r, 0, 3.141592653*2, 0)        CGContextSetRGBFillColor(ctx, 0.7, 0.7, 0.7, 1)        CGContextFillPath(ctx)                // 4,添加一个扇形表示进度        CGContextAddArc(ctx, r, r, r, 0, 3.141592653*2*_progressValue, 0)        CGContextAddLineToPoint(ctx, r, r)        CGContextSetRGBFillColor(ctx, 0, 0, 1, 1)                CGContextFillPath(ctx)    }
    private var _progressValue:CGFloat = 0        func getProgressValue()->CGFloat{        return _progressValue    }        func setProgressValue(value:CGFloat){        if value < 1{            _progressValue = value        }                setNeedsDisplay()    }}

2,ViewController

    @IBAction func addProgressBtnPressed(sender: AnyObject) {                pc.setProgressValue(pc.getProgressValue()+0.1)    }            override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.                pc = ProgressControl(frame: CGRect(x: 100, y: 100, width: 100, height: 100))        self.view.addSubview(pc)    }            private var pc:ProgressControl!






0 0
原创粉丝点击