【问题标题】:equivalent way drawing method to UIGraphicsGetCurrentContext与 UIGraphicsGetCurrentContext 等效的绘图方法
【发布时间】:2017-10-23 13:22:47
【问题描述】:

我有一个输出饼图的 swift 类,我想从 drawRect 方法中取出绘图逻辑,因为 UIGraphicsGetCurrentContext 在 draw 方法之外返回 nil 我需要更改绘制我的饼图视图的方式,

执行此操作的适当方法是什么?我的 PieChart 类看起来像;

override func draw(_ rect: CGRect) {
        let context: CGContext = UIGraphicsGetCurrentContext()!
        drawLinearGradient(context)
        drawCenterCircle(context)
        let circle: CAShapeLayer = drawStroke()
        let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
        pathAnimation.duration = percentage
        pathAnimation.toValue = percentage
        pathAnimation.isRemovedOnCompletion = rendered ? false : (percentageLabel > 20 ? false : true)
        pathAnimation.isAdditive = true
        pathAnimation.fillMode = kCAFillModeForwards
        circle.add(pathAnimation, forKey: "strokeEnd")
}

private func drawLinearGradient(_ context: CGContext) {
    let circlePoint: CGRect = CGRect(x: 0, y: 0,
                                     width: bounds.size.width, height: bounds.size.height)
    context.addEllipse(in: circlePoint)
    context.clip()

    let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
    let colorArray = [colors.0.cgColor, colors.1.cgColor]
    let colorLocations: [CGFloat] = [0.0, 1.0]
    let gradient = CGGradient(colorsSpace: colorSpace, colors: colorArray as CFArray, locations: colorLocations)

    let startPoint = CGPoint.zero
    let endPoint = CGPoint(x: 0, y: self.bounds.height)
    context.drawLinearGradient(gradient!,
                                start: startPoint,
                                end: endPoint,
                                options: CGGradientDrawingOptions.drawsAfterEndLocation)


}

private func drawCenterCircle(_ context: CGContext) {
    let circlePoint: CGRect = CGRect(x: arcWidth, y: arcWidth,
                                     width: bounds.size.width-arcWidth*2,
                                     height: bounds.size.height-arcWidth*2)

    context.addEllipse(in: circlePoint)
    UIColor.white.setFill()
    context.fillPath()
}

private func drawStroke() -> CAShapeLayer {
    let circle = CAShapeLayer()
    self.layer.addSublayer(circle)
    return circle
}

【问题讨论】:

  • 您已经尝试过方法了吗?如果是,您能否更具体地说明什么不起作用
  • draw(rect:) 方法根据定义有它自己的上下文。您是否尝试过创建一个新的?
  • 在 iOS 11 中以某种方式多次访问 drawRect 方法(iOS 10 和 9 没有问题),我必须将此逻辑移到除 drawRect 之外的其他地方。

标签: ios swift calayer drawrect


【解决方案1】:

您可以更改代码以生成包含饼图的UIImage,然后将该图像放在图像视图中:

UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
// ... here goes your drawing code, just as before
let pieImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let pieView = UIImageView(image: pieImage)

请注意,如果您需要非常频繁地更新图表(每秒多次),此解决方案可能会带来性能损失,因为图像创建比纯渲染花费更多时间。

【讨论】:

    猜你喜欢
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    相关资源
    最近更新 更多