Swift UISplitViewController中绘图的奇怪错误

Swift UISplitViewController中绘图的奇怪错误,swift,drawing,uinavigationbar,core-graphics,uistackview,Swift,Drawing,Uinavigationbar,Core Graphics,Uistackview,我正在尝试制作一个带有视图的屏幕,用户可以在其中绘制一些东西。我使用以下代码创建了自定义视图: override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { swiped = false if let touch = touches.first { lastPoint = touch.location(in: imageView) } } overrid

我正在尝试制作一个带有视图的屏幕,用户可以在其中绘制一些东西。我使用以下代码创建了自定义视图:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = false
    if let touch = touches.first {
        lastPoint = touch.location(in: imageView)
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = true
    if let touch = touches.first {
        let currentPoint = touch.location(in: imageView)
        drawLine(fromPoint: lastPoint, toPoint: currentPoint)

        lastPoint = currentPoint
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if !swiped {
        // draw a single point
        drawLine(fromPoint: lastPoint, toPoint: lastPoint)
    }
当我在视图控制器中显示视图时,一切正常:

但当我在UISplitViewController的详细视图中显示它时,当用户继续绘制时,部分已经绘制的图像会移动并淡出:

我在网上找不到任何关于什么bug的信息,也不知道是什么导致了这种行为

有人对此有什么想法吗

这就是您可以复制该bug的示例项目:


顺便说一句,在实际项目中,不仅拆分视图控制器的主视图,而且导航栏也会影响绘图

结果表明,它的行为是这样的,因为imageView框架的大小是分数部分

我刚刚将绘图上下文乘以2,问题就解决了:

func drawLine(fromPoint fromPoint: CGPoint, toPoint: CGPoint) {
    // multiply to avoid problems when imageView frame value is XX.5
    let fixedFrameForDrawing = CGRect(x: 0, y: 0, width: imageView.frame.size.width*2, height: imageView.frame.size.height*2)
    let point1 = CGPoint(x: fromPoint.x*2, y: fromPoint.y*2)
    let point2 = CGPoint(x: toPoint.x*2, y: toPoint.y*2)
    UIGraphicsBeginImageContext(fixedFrameForDrawing.size)
    if let context = UIGraphicsGetCurrentContext() {
        imageView.image?.draw(in: fixedFrameForDrawing)

        context.move(to: point1)
        context.addLine(to: point2)

        context.setLineCap(.round)
        context.setLineWidth(lineWidth*2)
        context.setStrokeColor(lineColor.cgColor)

        context.strokePath()

        let imageFromContext = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        imageView.image = imageFromContext
    }

这只是发生在我的应用程序中,疯狂的东西。现在我四舍五入的框架大小,它的作品很好。这一定很难弄清楚,谢谢你的解决方案!
func drawLine(fromPoint fromPoint: CGPoint, toPoint: CGPoint) {
    // multiply to avoid problems when imageView frame value is XX.5
    let fixedFrameForDrawing = CGRect(x: 0, y: 0, width: imageView.frame.size.width*2, height: imageView.frame.size.height*2)
    let point1 = CGPoint(x: fromPoint.x*2, y: fromPoint.y*2)
    let point2 = CGPoint(x: toPoint.x*2, y: toPoint.y*2)
    UIGraphicsBeginImageContext(fixedFrameForDrawing.size)
    if let context = UIGraphicsGetCurrentContext() {
        imageView.image?.draw(in: fixedFrameForDrawing)

        context.move(to: point1)
        context.addLine(to: point2)

        context.setLineCap(.round)
        context.setLineWidth(lineWidth*2)
        context.setStrokeColor(lineColor.cgColor)

        context.strokePath()

        let imageFromContext = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        imageView.image = imageFromContext
    }