swift3.0 实现自定义圆环进度提示效果

swift3.0改动很大,这里参考博客 :http://www.jb51.net/article/84548.htm 使用swift3.0完成自定义环形进度条提示效果。

展示效果:


代码:

自定义view

import UIKit

class FFView: UIView {
    var value: CGFloat = 0 {
        didSet {
            self.setNeedsDisplay()
        }
    }
    
    var maximumValue: CGFloat = 0 {
        didSet { self.setNeedsDisplay() }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.red
        self.isOpaque = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
        // Drawing code
        super.draw(rect)
        
        
        //线宽度
        let lineWidth: CGFloat = 10.0
        //半径
        let radius = rect.width / 2.0 - lineWidth
        //中心点x
        let centerX = rect.midX
        //中心点y
        let centerY = rect.midY
        //弧度起点
        let startAngle = CGFloat(-90 * M_PI / 180)
        //弧度终点
        let endAngle = CGFloat(((self.value / self.maximumValue) * 360.0 - 90.0) ) * CGFloat(M_PI) / 180.0
        
        //创建一个画布
        let context = UIGraphicsGetCurrentContext()
        
        //画笔颜色
        context!.setStrokeColor(UIColor.blue.cgColor)
        
        //画笔宽度
        context!.setLineWidth(lineWidth)
        
        //(1)画布 (2)中心点x(3)中心点y(4)圆弧起点(5)圆弧结束点(6) 0顺时针 1逆时针
        context?.addArc(center: CGPoint(x:centerX,y:centerY), radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: false)
        
        //绘制路径
        context!.strokePath()
        
        //画笔颜色
        context!.setStrokeColor(UIColor.darkGray.cgColor)
        
        //(1)画布 (2)中心点x(3)中心点y(4)圆弧起点(5)圆弧结束点(6) 0顺时针 1逆时针
        context?.addArc(center: CGPoint(x:centerX,y:centerY), radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        
        //绘制路径
        context!.strokePath()
    }

}

在控制器中使用:

import UIKit

class ViewController: UIViewController {

    let cireView = FFView.init(frame: CGRect(x:10.0,y:10.0,width : 100.0,height : 100.0))//.newAutoLayoutView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
           creatCire()
        
        // Do any additional setup after loading the view, typically from a nib.
    }
    func creatCire(){
        self.view.addSubview(cireView)
        self.cireView.value = 2
        self.cireView.maximumValue = 100
        self.cireView.backgroundColor = UIColor.yellow
        self.cireView.frame = CGRect(x:100, y:100, width:100,height: 100)
        wangmumu()
    }
    func wangmumu(){
        
        self.cireView.value += 2
        
        if self.cireView.value == 90 {return}
        
        self.perform(#selector(ViewController.wangmumu), with: self, afterDelay: 0.2)
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    


}

源码地址: https://github.com/feifeiios/Test

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值