drawRect,绘制项目并忽略掩码

时间:2018-04-08 14:10:07

标签: ios swift uiview drawrect

您好我正在尝试在drawRect中创建图表。目前图表如下所示:

enter image description here

正如您所看到的那样,“今天”正在被渐变蒙版切断。我像这样创建了面具:

        // Gradient Fill
        let context = UIGraphicsGetCurrentContext()!
        let colors = [gradStartColor.cgColor, gradEndColor.cgColor] as CFArray
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let colorLocations: [CGFloat] = [0.0, 1.0]
        let gradient = CGGradient(colorsSpace: colorSpace, colors: colors, locations: colorLocations)!
        let startPoint: CGPoint = CGPoint(x: 0, y: maxY(rect: rect))
        let endPoint: CGPoint = CGPoint(x: 0, y: height)
        let clippingPath: UIBezierPath = linePath.copy() as! UIBezierPath
        clippingPath.addLine(to: CGPoint(x: coords[coords.count - 1].x, y: height))
        clippingPath.addLine(to: CGPoint(x: coords[0].x, y: height))
        clippingPath.close()
        clippingPath.addClip()
        context.drawLinearGradient(gradient, start: startPoint, end: endPoint, options: [])

我的问题是如何在图形顶部绘制项目而忽略蒙版以便它们不会被切断?

1 个答案:

答案 0 :(得分:1)

您可以在裁剪操作之前保存当前图形状态,然后将其恢复,以便裁剪不再有效。

context.saveGState()
//do clipped drawing
context.restoreGState()
//do further drawing