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

时间:2016-08-15 11:20:40

标签: ios iphone swift uiview label

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

2 个答案:

答案 0 :(得分:8)

您可以使用以下代码实现此目标,只需调整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)

答案 1 :(得分:0)

我已经在 Swift 5 中编写了用于执行此操作的自定义函数。我相信我会为某人节省很多时间。玩得开心。

enter image description here

-  content_for :scripts do
  :javascript
    const input = document.querySelector('#input-tags');
    $("#input-tags").select2({
      tags: true,
      tokenSeparators: [','],
    });

  = f.input :code_list,  input_html: {id: 'input-tags', multiple: 'multiple'}, collection: @entry.code_list