iOS中的drawRect

Posted

技术标签:

【中文标题】iOS中的drawRect【英文标题】:drawRect in iOS 【发布时间】:2013-10-22 02:30:12 【问题描述】:

我有一个简单的例子,自定义 UIView 绘制一条带有刻度刻度线 (0 - 8000) 的线。单击时我有一个测试按钮,刻度线范围从(0-5000)变化,我强制 setNeedsDisplay 重新绘制刻度线的新标签。我确实在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);


  

【问题讨论】:

你是什么意思“我确实在drawrect中达到了新值”?你在drawrect中设置断点?或者做一个值的 NSLog? 【参考方案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);

【讨论】:

哦,您是否将 CGContextBeginPath 排除在您复制到上面答案中的代码之外? 因为它不存在。它必须在那里。 CGContextBeginPath(context);这行的哪一部分没有显示在代码中?

以上是关于iOS中的drawRect的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发:iOS中的多控制器管理

IOS5中的Safari不兼容Javascript中的Date问题

Java中的IO流

iOS6中的iOS行车路线

java中的IO 的示例

iOS中的NSLineSeparatorCharacter?