UIGraphicsGetCurrentContext的上下线颜色不正确

时间:2018-06-26 16:56:52

标签: ios swift uigraphicscontext asyncdisplaykit cgcolor

该项目使用ASDK(纹理)。有一个主节点,它有一个子节点,在某些情况下,有必要显示子节点的外边界,在此项目中,它是通过添加边界节点和UIGraphicsGetCurrentContext来完成的,任务是边界无论UIScreen.main.scale,所有设备上的线宽都相同。我做了一个测试代码,除了下部和上部线条的宽度小于侧面线(正确显示的宽度)之外,其他所有东西都工作正常。请告知如何解决?我无法散布整个屏幕的屏幕快照,但是我上传了该部分,以便可以显示问题所在。您可以在底行看到绿色,应该不是。

enter image description here

private func enableEditingMode() {
        let solidBorderStyle = BorderStyle.solid
        let border = ASBorderNode(shapeType: self.shapeType, borderType: solidBorderStyle)
        border.frame = ASBorderNode.calculateFrame(self.shapeType, borderType: solidBorderStyle, superView: frame)
        supernode?.insertSubnode(border, at: 0)
    }

enum BorderStyle {
    case dashed
    case solid
    case solidLight

    var lineWidth: CGFloat {
        switch self {
        case .dashed:
            return 1
        case .solid:
            return 6 / UIScreen.main.scale
        case .solidLight:
            return 2
        }
    }

}

class ASBorderNode: ASDisplayNode {

    private (set) var shapeType: TemplateLayoutElement.Shape?
    private (set) var borderType: BorderStyle


    init(shapeType:TemplateLayoutElement.Shape?, borderType:BorderStyle = .dashed) {
        self.borderType = borderType
        self.shapeType = shapeType
        super.init()
        self.isOpaque = false
    }

    class func calculateFrame(_ shapeType: TemplateLayoutElement.Shape?, borderType: BorderStyle, superView frame: CGRect) -> CGRect {
        switch borderType {
        case .solid:
            let lineWidth = borderType.lineWidth //* UIScreen.main.scale
            let widthHeightValue = lineWidth * 2
            return CGRect(x: -lineWidth + frame.origin.x, y: -lineWidth + frame.origin.y, width: frame.width + widthHeightValue, height: frame.height + widthHeightValue)
        default:
            assertionFailure("Implement for specific style")
            return .zero
        }
    }

    override class func draw(_ bounds: CGRect, withParameters parameters: Any?, isCancelled isCancelledBlock: () -> Bool, isRasterizing: Bool) {
        if let context = UIGraphicsGetCurrentContext(),
            let parameters = parameters as? ASBorderNode {

            switch parameters.borderType {
            case .dashed:
                break
            case .solid:
                context.setStrokeColor(UIColor.black.withAlphaComponent(0.5).cgColor)
                context.setFillColor(UIColor.clear.cgColor)
                context.setLineWidth(parameters.borderType.lineWidth)
            case .solidLight:
                break
            }

            switch parameters.shapeType {
            case .circle?:
                break
            default:
                switch parameters.borderType {
                case .solid:
                    debugPrint("Bounds", bounds)
                    let frame = bounds.insetBy(dx: parameters.borderType.lineWidth / UIScreen.main.scale, dy: parameters.borderType.lineWidth / UIScreen.main.scale)
                    debugPrint("insetBy", frame)
                    context.addRect(frame)
                default:
                    break
                }
            }

            context.strokePath()
        }
    }

    override func drawParameters(forAsyncLayer layer: _ASDisplayLayer) -> NSObjectProtocol? {
        return self
    }

}

更新1 :另一个示例 enter image description here

1 个答案:

答案 0 :(得分:1)

我相信您看到的是中风伪影。我知道如何避免它们的唯一方法是建立一个与您想要的笔触形状相对应的路径,并使用fill(因此,您要使填充的对象彼此叠置,而不仅仅是进行笔划基本路径)。