首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >drawRect in iOS

drawRect in iOS
EN

Stack Overflow用户
提问于 2013-10-22 02:30:12
回答 1查看 5.4K关注 0票数 0

我有一个简单的例子,自定义UIView,它用刻度刻度标记(0-8000)画一条线。我有一个测试按钮,当我点击,勾标范围从(0-5000),我强迫setNeedsDisplay重新绘制新标签的滴答标志。我确实按下了新的值,但它从不重新绘制新标签,它只在第一次启动时绘制新标签。有线索吗?这是密码。

代码语言:javascript
复制
- (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);


  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-22 04:06:52

您需要有一个CGContextBeginPath与您的CGContextStrokePath一起使用。

每次绘图时,您都应该从CGContextBeginPath(上下文)开始,以CGContextStrokePath(上下文)结尾;

代码语言:javascript
复制
- (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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19508031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档