2017-02-09 107 views
0

我想讓我的用戶精確選擇他們在UIImage上觸摸點。 我在左上角有一個放大廣場,用2倍變焦顯示他們在哪裏觸摸。它運作良好。貝塞爾路徑不繪製上下文

我試圖在放大區域的中心添加一個「十字準線」,使選擇更清晰。

下面的代碼沒有可見的行。

//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() 
} 

,我希望它是這有可能是線路正在制定,但過小或沒有。

我試着畫線時喜歡用CGContextAddPath

的其他方面,我認爲這個問題可能涉及到renderInContextView不聽我拉進去,或者說我沒有正確添加路徑上下文?

放大代碼基於GJNeilson's work,我所做的所有操作都是將放大鏡​​的中心點固定到左上方並取下面罩。

回答

1

我想你正在畫線,然後在上面畫圖。最後嘗試撥打drawLine

另外,縮放和平移在繪製可能將其置於屏幕外的線條時仍處於活動狀態。您可能需要使用CGContextSaveGStateCGContextRestoreGState

+0

重設它們,它們聽起來都很不錯!我的Macbook在我的背部轉動時開始更新!當我嘗試過時,我會告訴你。 – ThrowingSpoon

+0

這是保存和恢復狀態,使其工作。正如你所說,它一定是在其他地方定位它。 謝謝你的時間。 – ThrowingSpoon