【问题标题】:drawRect, drawing items and ignoring the maskdrawRect,绘制项目并忽略遮罩
【发布时间】:2018-09-18 00:19:06
【问题描述】:

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

如您所见,“今天”文本被渐变蒙版截断。我像这样创建面具:

        // 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: [])

我的问题是如何在图表顶部绘制项目,同时忽略蒙版,以免它们被截断?

【问题讨论】:

    标签: ios swift uiview drawrect


    【解决方案1】:

    可以在剪裁操作前保存当前图形状态,之后再恢复,使剪裁不再处于活动状态。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 2015-03-23
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      相关资源
      最近更新 更多