You need to enable JavaScript to run this app. 如何让圆角视图进度条的UIBezierPath从顶部中点而非左上角开始
优惠活动
大模型
产品
解决方案
定价
更多

如何让圆角视图进度条的UIBezierPath从顶部中点而非左上角开始

圆角视图进度条起点调整方案

问题原因

使用系统自带的UIBezierPath(roundedRect:cornerRadius:)接口生成的圆角矩形路径,默认起始点为左上角,因此进度条会默认从该位置开始绘制。

修改方案

手动构建从顶部中点为起点的圆角矩形路径即可,无需调整原有动画逻辑,完整修改后的代码如下:

func buildRoundView1(roundView: UIView, total: Int, borderColor: UIColor, shapeLayer: CAShapeLayer){
    let trackLayer = CAShapeLayer()
    
    let rect = CGRect(x: 0, y: 0, width: roundView.frame.size.width, height: roundView.frame.size.height)
    let cornerRadius: CGFloat = 10
    // 手动构建从顶部中点开始的圆角矩形路径
    let circularPath = UIBezierPath()
    // 起点设为顶部中点
    circularPath.move(to: CGPoint(x: rect.midX, y: rect.minY))
    // 绘制顶部右半段+右上角圆角
    circularPath.addLine(to: CGPoint(x: rect.maxX - cornerRadius, y: rect.minY))
    circularPath.addArc(withCenter: CGPoint(x: rect.maxX - cornerRadius, y: rect.minY + cornerRadius), radius: cornerRadius, startAngle: -.pi/2, endAngle: 0, clockwise: true)
    // 绘制右侧边+右下角圆角
    circularPath.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY - cornerRadius))
    circularPath.addArc(withCenter: CGPoint(x: rect.maxX - cornerRadius, y: rect.maxY - cornerRadius), radius: cornerRadius, startAngle: 0, endAngle: .pi/2, clockwise: true)
    // 绘制底部边+左下角圆角
    circularPath.addLine(to: CGPoint(x: rect.minX + cornerRadius, y: rect.maxY))
    circularPath.addArc(withCenter: CGPoint(x: rect.minX + cornerRadius, y: rect.maxY - cornerRadius), radius: cornerRadius, startAngle: .pi/2, endAngle: .pi, clockwise: true)
    // 绘制左侧边+左上角圆角
    circularPath.addLine(to: CGPoint(x: rect.minX, y: rect.minY + cornerRadius))
    circularPath.addArc(withCenter: CGPoint(x: rect.minX + cornerRadius, y: rect.minY + cornerRadius), radius: cornerRadius, startAngle: .pi, endAngle: -.pi/2, clockwise: true)
    // 闭合路径回到顶部中点
    circularPath.addLine(to: CGPoint(x: rect.midX, y: rect.minY))
    
    trackLayer.path = circularPath.cgPath
    
    trackLayer.lineWidth = 4
    trackLayer.fillColor = UIColor.clear.cgColor
    trackLayer.lineCap = CAShapeLayerLineCap.round
    roundView.layer.addSublayer(trackLayer)
    shapeLayer.path = circularPath.cgPath
    
    shapeLayer.strokeColor = borderColor.cgColor
    shapeLayer.lineWidth = 4
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.lineCap = CAShapeLayerLineCap.round
    
    shapeLayer.strokeEnd = 0
    roundView.layer.addSublayer(shapeLayer)
    
    let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
    let startValue = Double(10800-total).normalize(min: 0.0, max: 10800.0)
    basicAnimation.fromValue = 1 - startValue
    basicAnimation.toValue = 1
    basicAnimation.duration = CFTimeInterval(total)
    basicAnimation.fillMode = CAMediaTimingFillMode.backwards
    basicAnimation.isRemovedOnCompletion = true
    
    shapeLayer.add(basicAnimation, forKey: "urSoBasic")
}

补充说明

如果后续需要调整圆角大小,直接修改cornerRadius变量的取值即可,无需调整路径绘制逻辑。

内容的提问来源于stack exchange,提问作者Syed Anas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 14:45:05