2016-04-30 55 views
0
之间的直线

我现在的OBJ-C代码:对象 - 问题画两点

- (void)slopeMode:(CGPoint)point{ 
    if (dPoint1.x != -1) //If a coordinate pos has already been set for dPoint1 
    { 
     dPoint2 = point; //Then set it in dPoint2 instead 


     [cPath moveToPoint: CGPointMake(dPoint1.x-5,dPoint1.y-5)]; 
     [cPath addLineToPoint:CGPointMake(dPoint2.x-5,dPoint2.y-5)]; 
     [cPath moveToPoint:CGPointMake(dPoint2.x-5,dPoint2.y-5)]; 

     [cPath addArcWithCenter:CGPointMake(dPoint2.x-5,dPoint2.y-5) radius:10 startAngle:0 endAngle:DEGREES_TO_RADIANS(360) clockwise:YES]; 

     [cPath closePath]; 
     [[UIColor greenColor] setStroke]; 
     [[UIColor greenColor] setFill]; 

     [cPath fill]; 
     [cPath stroke]; 

     gfxP1.path = [cPath CGPath]; 
     gfxP1.fillColor = [[UIColor greenColor] CGColor]; 
     gfxP1.lineWidth = 4.0; 

     [[wBView layer] addSublayer:gfxP1]; 

     rSlope = [self calcSlope:dPoint1 :dPoint2]; 

     [self callAlert:@"Slope Calculation" :[NSString stringWithFormat:@"%.*f",precision,rSlope] :@"OK"]; 

     //Reset datapoints 
     [self resetDPoint:&dPoint1]; 
     [self resetDPoint:&dPoint2]; 

     //Reset gfx 
     //[self resetCALayer:gfxP1]; 
     //[self resetCALayer:gfxP2]; 

     //Reset other vars 
     cSlope = -1; 
     rSlope = -1; 
     tempFStore = @""; 
    } 

    else 
    { 
     dPoint1 = point;   

     cPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(dPoint1.x-5,dPoint1.y-5) radius:10 startAngle:0 endAngle:DEGREES_TO_RADIANS(360) clockwise:YES];  
    } 
} 

我的问题是,我能到我的UIView对象,但行我想中显示我的两个点添加将这两点连接在一起不会被创建或不能正确显示。以下是我在IPhone模拟器使用上面的代码中看到:

2 dots enter, no line leaves

+0

参考这个链接,你可以得到一个想法http://stackoverflow.com/questions/8208469/draw-line-between-two-points-in-iphone和http://stackoverflow.com/questions/31775194 /创建行的扩展换给定行创建通过用户 –

回答

0

下面的代码尝试(做修改,按您的要求):

得到两个CGPoint并把它传递到下面的方法:

- (void)drawLine:(CGPoint)startPoint endPoint:(CGPoint)endPoint{ 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 
    CGContextSetStrokeColorWithColor(context, [[UIColor redColor]CGColor]);//adjust color 
    CGContextSetLineWidth(context, 1.0);//adjust line width 
    CGContextMoveToPoint(context, startPoint.x, startPoint.y); 
    CGContextAddLineToPoint(context, endPoint.x, endPoint.y); 
    CGContextStrokePath(context); 
    CGContextRestoreGState(context); 

}