扇形進(jìn)度繪制

扇形進(jìn)度繪制


Simulator Screen Recording - iPhone 14 - 2023-04-22 at 22.48.09.gif
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
        
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容