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