由下而上逐渐填充圆swift2

9nvpjoqh  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(201)

基本上我是非常新的雨燕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)
}

然而,我们如何在Swift 2中绘制水平填充的圆呢?我指的是填充的圆,例如,根据Swift代码中指定的百分比从下到上填充。
以下是我们需要的预览:

qvtsj1bj

qvtsj1bj1#

视图和形状图层绝对是一个麻烦的应用程序。你应该看看UIGraphicsBeginImageContextWithOptions或者iOS 10或更高版本的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()

相关问题