简体   繁体   English

贝塞尔曲线路径未根据上下文绘制

[英]Bezier Path Not Drawing on context

I'm trying give my user fine selection of a point they touch on a UIImage. 我正在尝试让我的用户更好地选择他们在UIImage上触摸的点。 I have a magnifying square in the top left corner that shows where they're touching at 2x zoom. 我在左上角有一个放大的正方形,以2倍缩放显示他们触摸的位置。 It works well. 它运作良好。

I'm trying to add a "crosshair" in the center of the magnifying area to make selection clearer. 我正在尝试在放大区域的中心添加“十字准线”,以使选择更加清晰。

With the code below no line is visible. 在下面的代码中,看不到任何行。

//Bulk of the magifying code    
public override func drawRect(rect: CGRect) {
        let context: CGContextRef = UIGraphicsGetCurrentContext()!
        CGContextScaleCTM(context, 2, 2)
        CGContextTranslateCTM(context, -self.touchPoint.x, -self.touchPoint.y)
        drawLine(context)
        self.viewToMagnify.layer.renderInContext(context)
}
//Code for drawing horizontal line of crosshair
private func drawLine(ctx: CGContext) {
    let lineHeight: CGFloat = 3.0
    let lineWidth: CGFloat = min(bounds.width, bounds.height) * 0.3
    let horizontalPath = UIBezierPath()
    horizontalPath.lineWidth = lineHeight
    let hStart = CGPoint(x:bounds.width/2 - lineWidth/2, y:bounds.height/2)
    let hEnd = CGPoint(x:bounds.width/2 + lineWidth/2,y:bounds.height/2)
    horizontalPath.moveToPoint(hStart)
    horizontalPath.addLineToPoint(hEnd)
    UIColor.whiteColor().setStroke()
    horizontalPath.stroke()
}

It's possible that the line is being drawn but too small or not where I expect it to be. 在这里我希望它是这有可能是线路正在制定,但过小或没有。

I've tried other ways of drawing the line like using CGContextAddPath 我尝试了其他方式来绘制线条,例如使用CGContextAddPath

I think the issue might be related to the renderInContextView not taking my drawing into account, or I'm not adding the path to the context correctly? 我认为问题可能与renderInContextView没有考虑到我的绘图有关,或者我没有正确添加到上下文的路径?

The magnification code is based on GJNeilson's work , all I've done is pin the centre point of the magnifying glass to the top left and remove the mask. 放大倍率代码基于GJNeilson的工作 ,我所做的只是将放大镜​​的中心点固定在左上方,然后取下遮罩。

I think you're drawing the line then drawing the image over it. 我认为您是先画线然后在其上画图像。 Try calling drawLine last. 尝试最后调用drawLine

Also, the scale and translate are still active when you draw the line which may be positioning it offscreen. 此外,在绘制可能将其置于屏幕外的线时,比例和平移仍处于活动状态。 You might have to reset it using CGContextSaveGState and CGContextRestoreGState 您可能必须使用CGContextSaveGStateCGContextRestoreGState重置它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM