扇形进度绘制

扇形进度绘制


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
        
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容