从下到上逐渐填充一个圆圈swift2

filling a circle gradually from bottom to top swift2

基本上我是 Swift 2 的新手,使用下面的代码创建了一个带有描边和白色背景的圆圈,然后我得到了一个类似这样的圆圈:

func getDynamicItemQty() -> UIImage {

    let View = UIView(frame: CGRectMake(0,0,200,200))

    let circlePath =
        UIBezierPath(arcCenter: CGPoint(x: 100,y: 100), radius: CGFloat(90), startAngle: CGFloat(9.4), endAngle:CGFloat(0), clockwise: false)

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circlePath.CGPath


    //shapeLayer.fillRule = kCAFillRuleEvenOdd
    //change the fill color
    shapeLayer.fillColor = UIColor.brownColor().CGColor
    //you can change the stroke color
    shapeLayer.strokeColor = UIColor.blueColor().CGColor
    //you can change the line width
    shapeLayer.lineWidth = 10

    View.layer.addSublayer(shapeLayer)

    return UIImage.renderUIViewToImage(View)
}

但是,在Swift2中,如何绘制水平部分填充的圆?我的意思是圆圈,例如,根据 Swift 代码中指定的百分比从底部到顶部填充。

这是我们需要的预览:

视图和形状图层绝对是错误的方法。你应该看看 UIGraphicsBeginImageContextWithOptions or for iOS 10 or newer UIGraphicsImageRenderer。对于您的问题:您应该画两次圆圈。类似的东西:

let size = CGSize(width: 200.0, height: 200.0)
UIGraphicsBeginImageContextWithOptions(size, true, 0)
let circlePath =
    UIBezierPath(arcCenter: CGPoint(x: 100, y: 100), radius: CGFloat(90), startAngle: CGFloat(9.4), endAngle:CGFloat(0), clockwise: false)

UIColor.white.fill()
UIRectFill(origin: CGPoint.zero, size: size)
// Drawing the background with a clipping
UIGraphicsPushContext(UIGraphicsGetCurrentContext())
UIColor(...).setFill()
UIRectClip(CGRect(x: 0.0, y:10.0 + 180.0 * (1.0 - percentage), width:size.width, height:size.height))
circlePath.fill()
// leave the subcontext to discard the clipping
UIGraphicsPopContext()
UIColor(...).setStroke()
circlePath.lineWidth = 10.0
circlePath.stroke()

// Keep the fruits of our labour
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()