扇形进度绘制
var timerFire: Timer?
@IBOutlet weak var timeLabel: UILabel!
var timerCount: CGFloat = 0
let allCount: CGFloat = 10
if self.timerFire != nil {
self.timerFire?.invalidate()
self.timerFire = nil
} else {
let timer = Timer.init(timeInterval: 0.1, repeats: true) { [weak self]item in
self?.repeatsClick(item: item)
}
self.timerCount = 0
RunLoop.current.add(timer, forMode: RunLoop.Mode.common)
timer.fire()
self.timerFire = timer
}
func repeatsClick(item: Timer) {
self.timerCount += 0.1
if self.timerCount >= allCount {
item.invalidate()
self.timerCount = allCount
self.timerFire?.invalidate()
self.timerFire = nil
}
let progress: CGFloat = CGFloat(Float(self.timerCount) / Float(allCount))
print("item === \(self.timerCount) \(progress) == \(Int(floorf(Float(self.timerCount))))" )
self.timeLabel.text = "\(Int(allCount) - Int(floorf(Float(self.timerCount))))"
self.progressView.progress = progress
}
class LSPKProgressView: UIView {
let shapeLayer = CAShapeLayer()
var progress: CGFloat = 0.5 {
didSet {
self.setNeedsDisplay()
}
}
override func draw(_ rect: CGRect) {
let center = CGPoint(x: rect.width / 2, y: rect.height / 2)
let offsetSpace: CGFloat = 0
let radius: CGFloat = min(rect.width, rect.height) / 2 - offsetSpace
let offset = CGFloat.pi / 2
let startAngle: CGFloat = 0 - offset
let endAngle: CGFloat = CGFloat.pi * 2 * self.progress - offset
let path = UIBezierPath()
path.move(to: center)
path.addArc(withCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.addLine(to: center)
path.close()
UIColor.red.setFill()
path.fill()
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = self.bounds.size.width / 2
self.clipsToBounds = true
}
}