iOS:在 UIView 中以百分比形式绘制圆圈

标签 ios iphone swift uiview label

我有一个圆形的 UIView,我需要以百分比值显示 UIView 边框颜色,如果百分比值为 50%,它应该填充一半边框颜色的 UIView。我使用了 UIBeizer 路径 addArcWithCenter 但是我没有得到完美的解决方案。请在这方面帮助我

最佳答案

你可以用下面的代码实现它,只需调整strokeStartstrokeEnd:

    // round view
    let roundView = UIView(frame: CGRectMake(100, 100, 250, 250))
    roundView.backgroundColor = UIColor.whiteColor()
    roundView.layer.cornerRadius = roundView.frame.size.width / 2

    // bezier path
    let circlePath = UIBezierPath(arcCenter: CGPoint (x: roundView.frame.size.width / 2, y: roundView.frame.size.height / 2),
                                  radius: roundView.frame.size.width / 2,
                                  startAngle: CGFloat(-0.5 * M_PI),
                                  endAngle: CGFloat(1.5 * M_PI),
                                  clockwise: true)
    // circle shape
    let circleShape = CAShapeLayer()
    circleShape.path = circlePath.CGPath
    circleShape.strokeColor = UIColor.redColor().CGColor
    circleShape.fillColor = UIColor.clearColor().CGColor
    circleShape.lineWidth = 1.5
    // set start and end values
    circleShape.strokeStart = 0.0
    circleShape.strokeEnd = 0.8

    // add sublayer
    roundView.layer.addSublayer(circleShape)
    // add subview
    self.view.addSubview(roundView)

关于iOS:在 UIView 中以百分比形式绘制圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38954110/

相关文章:

ios - 复杂的 NSPredicates : comparing values on the most-recent object

ios - AVPlayer 在后台停止

iphone - 为什么 XCode Controller 前缀是 xxxViewController 而不是 xxxController?

swift - Swift 中的 UILabel 根据行数而变化

ios - 动画 View 约束而不动画其他 View

Python struct.unpack ("i", bytearray(data)) 到 Swift 4

ios - 具有透明背景的模态 UISplitViewController

ios - 每个约束项都必须是 View 或布局指南

如果没有返回行,iPhone SQLite 查询会崩溃

iphone - 如何设置tableview背景颜色?