快速绘制自定义圆

标签 swift geometry uibezierpath

<分区>

我正在尝试实现自定义循环分析 View 。 View 应该是圆形的但被切断。

目标:

enter image description here

我的代码:

let circlePath = UIBezierPath(ovalIn: CGRect(x: innerRect.minX, y: innerRect.minY, width: innerRect.width, height: innerRect.height))

if trackBackgroundColor != UIColor.clear {
    trackBackgroundColor.setFill()
    circlePath.fill();
}
if trackBorderWidth > 0 {
    circlePath.lineWidth = trackBorderWidth
    trackBorderColor.setStroke()
    circlePath.stroke()
}    

// progress Drawing
let progressPath = UIBezierPath()
let progressRect: CGRect = CGRect(x: innerRect.minX, y: innerRect.minY, width: innerRect.width, height: innerRect.height)
let center = CGPoint(x: progressRect.midX, y: progressRect.midY)
let radius = progressRect.width / 2.0

let startAngle:CGFloat = clockwise ? CGFloat(-internalProgress * Double.pi / 180.0) : CGFloat(constants.twoSeventyDegrees * Double.pi / 180)
let endAngle:CGFloat = clockwise ? CGFloat(constants.twoSeventyDegrees * Double.pi / 180) : CGFloat(-internalProgress * Double.pi / 180.0)

progressPath.addArc(withCenter: center, radius:radius, startAngle:startAngle, endAngle:endAngle, clockwise:!clockwise)

当前输出:

enter image description here

如何绘制描述为目标的自定义圆圈。

最佳答案

这是您所需内容的粗略实现。这将绘制两条弧线。

class CircularProgressView: UIView {
    var trackBackgroundColor = UIColor.lightGray
    var trackBorderWidth: CGFloat = 10
    var progressColor = UIColor.red
    var percent: Double = 0 {
        didSet {
            setNeedsDisplay()
        }
    }

    // Adjust these to meet your needs. 90 degrees is the bottom of the circle
    static let startDegrees: CGFloat = 120
    static let endDegrees: CGFloat = 60

    override func draw(_ rect: CGRect) {
        let startAngle: CGFloat = radians(of: CircularProgressView.startDegrees)
        let endAngle: CGFloat = radians(of: CircularProgressView.endDegrees)
        let progressAngle = radians(of: CircularProgressView.startDegrees + (360 - CircularProgressView.startDegrees + CircularProgressView.endDegrees) * CGFloat(max(0.0, min(percent, 1.0))))

        let center = CGPoint(x: bounds.midX, y: bounds.midY)
        let radius = min(center.x, center.y) - trackBorderWidth / 2 - 10

        print(startAngle, endAngle, progressAngle)
        let trackPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        let progressPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: progressAngle, clockwise: true)
        trackPath.lineWidth = trackBorderWidth
        trackPath.lineCapStyle = .round
        progressPath.lineWidth = trackBorderWidth
        progressPath.lineCapStyle = .round

        trackBackgroundColor.set()
        trackPath.stroke()

        progressColor.set()
        progressPath.stroke()
    }

    private func radians(of degrees: CGFloat) -> CGFloat {
        return degrees / 180 * .pi
    }
}

let progress = CircularProgressView(frame: CGRect(x: 0, y: 0, width: 500, height: 400))
progress.backgroundColor = .white
progress.percent = 0.95

关于快速绘制自定义圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51142706/

相关文章:

ios - arc4random_uniform 在 OSX 10.10.4 上的 Xcode 7.0 beta (7a176x) 中不可用

ios - UILabel/UITextField 不使用异步 HTTP 请求更新

ios - 如何在 Swift 中屏蔽 UIButton?

uiview - 如何 'animate' 在 IOS 中改变 View 的形状? (即改变它的路径从一个到另一个)

ios - Swift 错误线程(一些数字): signal SIGABRT

ios - 无法将类型值分配给 UITableViewDataSource 类型

c++ - 如何确保给定的一组点位于可能正方形的边界上?

algorithm - 从 GPS 坐标记录计算圈数

python - 不断缩放 3 个圆直到它们相交

ios - UIView 中的 UIBezierPath 动画