2013-10-22 60 views
0

我有一個簡單的例子,自定義的UIView繪製一個帶刻度刻度線(0 - 8000)的線。點擊時我有一個測試按鈕,刻度標記範圍從(0-5000)變化,我強制setNeedsDisplay重新繪製刻度標記的新標籤。我確實以正確的方式創建了新的值,但它不會重新繪製新的標籤,它只會在首次啓動時繪製新的標籤。任何線索?這是代碼。在iOS中的drawRect

- (void)drawRect:(CGRect)rect //TESTTTTTTTTT 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    if(test){ 
    // [self setClearsContextBeforeDrawing: YES]; 
    high1=5000; 
    high2 = 1000; 
} 


high1 = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MaxScale1"] doubleValue]; 
high2 = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MaxScale2"] doubleValue]; 

CGFloat x1,x2,x3; 
CGFloat y1top, y2top,y3top; 
CGFloat y1bot, y2bot,y3bot; 
x1=63; 
x2=76; 
x3 = x1-5; 
float ht = 96; 

y1top = y2top = y3top= 114; 
y1bot = y2bot= y3bot = y1top+ht; 

////Gauge1 ///// 
float w1 = 308; //gaugeView1.size.width; 
float x1Start=5; 
float y1 = 90; 
float div1= w1/4.0; 
CGContextMoveToPoint(context,x1Start, y1); 
CGContextAddLineToPoint(context, x1Start, y1+5); 
[[UIColor whiteColor] set]; 
CGRect labrect = CGRectMake(x1Start-2,y1+5, 18, 7); 
[[NSString stringWithFormat:@"%d",0] drawInRect:labrect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:7.5]]; 

CGContextMoveToPoint(context,x1Start+div1, y1); 
CGContextAddLineToPoint(context, x1Start+div1, y1+5); 
[[UIColor whiteColor] set]; 
labrect = CGRectMake(x1Start+div1-9,y1+5, 18, 7); 
[[NSString stringWithFormat:@"%d",(int)(0.25*high1)] drawInRect:labrect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:7.5]]; 

CGContextMoveToPoint(context,x1Start+2*div1, y1); 
CGContextAddLineToPoint(context, x1Start+2*div1, y1+5); 
[[UIColor whiteColor] set]; 
labrect = CGRectMake(x1Start+2*div1-9,y1+5, 18, 7); 
[[NSString stringWithFormat:@"%d",(int)(0.5*high1)] drawInRect:labrect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:7.5]]; 

CGContextMoveToPoint(context,x1Start+3*div1, y1); 
CGContextAddLineToPoint(context, x1Start+3*div1, y1+5); 
[[UIColor whiteColor] set]; 
labrect = CGRectMake(x1Start+3*div1-9,y1+5, 18, 7); 
[[NSString stringWithFormat:@"%d",(int)(0.75*high1)] drawInRect:labrect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:7.5]]; 

CGContextMoveToPoint(context,x1Start+4*div1, y1); 
CGContextAddLineToPoint(context, x1Start+4*div1, y1+5); 
[[UIColor whiteColor] set]; 
labrect = CGRectMake(x1Start+4*div1-9,y1+5, 18, 7); 
[[NSString stringWithFormat:@"%d",(int)(high1)] drawInRect:labrect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:7.5]]; 

// CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); 
// CGContextSetLineWidth(context, 2.0); 
CGContextStrokePath(context); 


    } 
+0

你是什麼意思「我確實擊中了新的值」?你正在設置一個斷點?或者做一個NSLog的值? – HalR

回答

1

你需要有一個CGContextBeginPath來跟隨你的CGContextStrokePath。

每次繪製時,都應該從CGContextBeginPath(context)開始;並以CGContextStrokePath(context)結束;

- (void)drawRect:(CGRect)rect //TESTTTTTTTTT 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    if(test){ 
    // [self setClearsContextBeforeDrawing: YES]; 
    high1 = 5000; 
    high2 = 1000; 
    } 


    high1 = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MaxScale1"] doubleValue]; 
    high2 = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MaxScale2"] doubleValue]; 

    CGFloat x1 ,x2, x3; 
    CGFloat y1top, y2top, y3top; 
    CGFloat y1bot, y2bot, y3bot; 
    x1 = 63; 
    x2 = 76; 
    x3 = x1 - 5; 
    float ht = 96; 

    y1top = y2top = y3top= 114; 
    y1bot = y2bot= y3bot = y1top + ht; 

////Gauge1 ///// 
    float w1 = 308; //gaugeView1.size.width; 
    float x1Start = 5; 
    float y1 = 90; 
    float div1= w1/4.0; 

    CGContextBeginPath(context); 
    CGContextMoveToPoint(context, x1Start, y1); 
    CGContextAddLineToPoint(context, x1Start, y1 + 5); 
    [[UIColor whiteColor] set]; 
    CGRect labrect = CGRectMake(x1Start - 2, y1 + 5, 18, 7); 
    [[NSString stringWithFormat:@"%d", 0] drawInRect:labrect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:7.5]]; 

    CGContextMoveToPoint(context ,x1Start + div1, y1); 
    CGContextAddLineToPoint(context, x1Start + div1, y1 + 5); 
    [[UIColor whiteColor] set]; 
    labrect = CGRectMake(x1Start + div1 - 9,y1 + 5, 18, 7); 
    [[NSString stringWithFormat:@"%d",(int)(0.25 * high1)] drawInRect:labrect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:7.5]]; 

    CGContextMoveToPoint(context, x1Start + 2 * div1, y1); 
    CGContextAddLineToPoint(context, x1Start + 2 * div1, y1 + 5); 
    [[UIColor whiteColor] set]; 
    labrect = CGRectMake(x1Start + 2 * div1 - 9, y1 + 5, 18, 7); 
    [[NSString stringWithFormat:@"%d",(int)(0.5*high1)] drawInRect:labrect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:7.5]]; 

    CGContextMoveToPoint(context, x1Start + 3 * div1, y1); 
    CGContextAddLineToPoint(context, x1Start + 3 * div1, y1 + 5); 
    [[UIColor whiteColor] set]; 
    labrect = CGRectMake(x1Start + 3 * div1 - 9, y1 + 5, 18, 7); 
    [[NSString stringWithFormat:@"%d",(int)(0.75*high1)] drawInRect:labrect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:7.5]]; 

    CGContextMoveToPoint(context, x1Start + 4 * div1, y1); 
    CGContextAddLineToPoint(context, x1Start + 4 * div1, y1 + 5); 
    [[UIColor whiteColor] set]; 
    labrect = CGRectMake(x1Start + 4 * div1 - 9, y1 + 5, 18, 7); 
    [[NSString stringWithFormat:@"%d",(int)(high1)] drawInRect:labrect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:7.5]]; 

    CGContextStrokePath(context); 
} 
+0

嗯,在代碼中。 – IronMan1980

+1

哦,您是否將CGContextBeginPath從您複製到上述答案中的代碼中移出? – HalR

+1

因爲它不存在。它需要在那裏。 – HalR