2016-02-21 143 views
1

我正在開發一個應用程序,用戶可以在其中繪製線條。同一個應用程序的桌面版本可以在負座標上繪製線條,我也希望在iOS應用程序中具有相同的功能。iOS - 在負座標上繪製二維線條

我到目前爲止所嘗試的: - 我有一個UIViewController其中我已經覆蓋- (void)drawRect:(CGRect)rect並畫出了線。這裏是我使用的代碼: -

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextClearRect(context, self.bounds); 

CGPoint startPoint = CGPointMake(self.startHandle.frame.origin.x + self.startHandle.frame.size.width/2, self.startHandle.frame.origin.y + self.startHandle.frame.size.height/2); 

CGPoint endPoint = CGPointMake(self.endHandle.frame.origin.x + self.endHandle.frame.size.width/2, self.endHandle.frame.origin.y + self.endHandle.frame.size.height/2); 

CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); 
CGContextSetLineWidth(context, 2.0f); 
CGContextMoveToPoint(context, startPoint.x, startPoint.y); //start at this point 
CGContextAddLineToPoint(context, endPoint.x, endPoint.y); //draw to this point 

它工作得很好,直到我有startPoint.x爲負值。用負值,我沒有看到0,0座標後面的那一行。任何幫助或信息表示讚賞。

回答

0
CGContextClearRect(context, self.bounds); 

你可以畫出這個邊界,請做出更大的矩形。

+0

你的意思是改變self.bounds.origin.x爲負值? –

+0

@AnkurArora我的意思是帆布很小,你可以在外面畫一個較大的畫布,其起源是負面的。 – Lumialxk

+0

我無法理解你的意思。如果我增加畫布大小,這將有助於在負座標上繪製線條?因爲用畫布尺寸點(0,0)也會改變。你可以分享一個示例代碼來解釋你想要解釋的內容嗎? –

2

從@matt的說法(他已經刪除了他的答案,因爲它仍然有用而感到羞恥)之後,您只需要對上下文應用變換以便繪製標準笛卡爾座標系,其中(0,0)是屏幕的中心。

drawRect提供給您的上下文起源於屏幕的左上角,正y方向沿着屏幕向下。

enter image description here

所以首先,我們一定想先翻轉Y軸,使正y方向上升的屏幕。

我們可以使用CGContextScaleCTM(context, 1, -1)來做到這一點。這將反映上下文的y軸。因此,這個反映的上下文將有一個y軸,正方向在屏幕上方。

enter image description here

接下來,我們要翻譯的情況下,以正確的位置,使(0,0)對應於屏幕的中心。因此,我們應該將y軸向下移動一半高度(這樣y軸上的0現在位於中心),x軸向右移動寬度的一半。

我們可以通過使用CGContextTranslateCTM(context, width*0.5, -height*0.5),其中widthheight分別是你的觀點的寬度和高度,做到這一點。

現在您的上下文座標將是這樣的:

enter image description here

您可以實現到您的drawRect像這樣的:

-(void) drawRect:(CGRect)rect { 

    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    CGFloat width = self.bounds.size.width; 
    CGFloat height = self.bounds.size.height; 

    // scale and translate to the standard cartesian coordinate system where the (0,0) is the center of the screen. 
    CGContextScaleCTM(ctx, 1, -1); 
    CGContextTranslateCTM(ctx, width*0.5, -height*0.5); 

    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor); 
    CGContextSetLineWidth(ctx, 2.0); 

    // add y-axis 
    CGContextMoveToPoint(ctx, 0, -height*0.5); 
    CGContextAddLineToPoint(ctx, 0, height*0.5); 

    // add x-axis 
    CGContextMoveToPoint(ctx, -width*0.5, 0); 
    CGContextAddLineToPoint(ctx, width*0.5, 0); 

    // stroke axis 
    CGContextStrokePath(ctx); 


    // define start and end points 
    CGPoint startPoint = CGPointMake(-100, 100); 
    CGPoint endPoint = CGPointMake(100, -200); 

    CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor); 
    CGContextSetLineWidth(ctx, 5.0); 
    CGContextMoveToPoint(ctx, startPoint.x, startPoint.y); 
    CGContextAddLineToPoint(ctx, endPoint.x, endPoint.y); 

    CGContextStrokePath(ctx); 
} 

這會給下面的輸出:

enter image description here

我添加了紅線代表y和x軸,以便闡明我們在這裏所做的工作。

+0

這幫忙@ Ankur-Arora? – Hamish