所以我需要绘制2条不同的线条.通过另一个帖子,我想出了如何重新绘制一条线.我的问题是,如果我想绘制2行,我需要有2个代码段来做吗?或者有没有办法绘制n行?
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
NSLog(@"drawRect called");
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y);
CGContextAddLineToPoint(context, self.endPoint.x, self.endPoint.y);
CGContextStrokePath(context);
}
Run Code Online (Sandbox Code Playgroud)
履行
draw2D *myCustomView = [[draw2D alloc] init];
myCustomView.startPoint = CGPointMake(0, 0);
myCustomView.endPoint = CGPointMake(300, 300);
myCustomView.lineWidth = 5;
myCustomView.lineColor = [UIColor redColor];
myCustomView.frame = CGRectMake(0, 0, 500, 500);
[myCustomView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:myCustomView];
[myCustomView setNeedsDisplay];
myCustomView.endPoint = CGPointMake(100, 100);
myCustomView.lineColor = [UIColor orangeColor];
[myCustomView setNeedsDisplay];
Run Code Online (Sandbox Code Playgroud)
这只会重新划线.我会为我想要的每一行重复drawRect块吗?所以,如果我想要两条线,我必须这样做:
- (void)drawRect:(CGRect)rect
{
//line1
CGContextRef context1 = UIGraphicsGetCurrentContext();
NSLog(@"drawRect called");
CGContextSetLineWidth(context1, self.lineWidth1);
CGContextSetStrokeColorWithColor(context1, self.lineColor1.CGColor);
CGContextMoveToPoint(context1, self.startPoint1.x, self.startPoint1.y);
CGContextAddLineToPoint(context1, self.endPoint1.x, self.endPoint1.y);
CGContextStrokePath(context1);
//line 2
CGContextRef context2 = UIGraphicsGetCurrentContext();
NSLog(@"drawRect called");
CGContextSetLineWidth(context2, self.lineWidth2);
CGContextSetStrokeColorWithColor(context2, self.lineColor2.CGColor);
CGContextMoveToPoint(context2, self.startPoint2.x, self.startPoint2.y);
CGContextAddLineToPoint(context2, self.endPoint2.x, self.endPoint2.y);
CGContextStrokePath(context2);
}
Run Code Online (Sandbox Code Playgroud)
还是有更好的方法来处理我的线条?
编辑:最终目标是使用它UIView与标签和文本框我用作表格.我想要线条来分解表格.
我想知道我是否应该只使用这些自定义draw2D UIView作为行,并将它们放在UIView表单的顶部,然后我可以创建多个"draw2D" UIViews?
解:
所以这里是我认为可行的代码:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:[NSValue valueWithCGPoint:CGPointMake(10, 10)] forKey:@"start"];
[tempDict setValue:[NSValue valueWithCGPoint:CGPointMake(500, 500)] forKey:@"end"];
[tempArray addObject:tempDict];
NSMutableDictionary *tempDict2 = [[NSMutableDictionary alloc] init];
[tempDict2 setValue:[NSValue valueWithCGPoint:CGPointMake(400, 10)] forKey:@"start"];
[tempDict2 setValue:[NSValue valueWithCGPoint:CGPointMake(500, 500)] forKey:@"end"];
[tempArray addObject:tempDict2];
self.pointArray = [NSArray arrayWithArray:tempArray];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
for (int i = 0; i < [self.pointArray count]; i++)
{
CGPoint tempPoint = [[[self.pointArray objectAtIndex:i] objectForKey:@"start"] CGPointValue];
CGPoint tempPoint2 = [[[self.pointArray objectAtIndex:i] objectForKey:@"end"] CGPointValue];
CGContextMoveToPoint(context, tempPoint.x, tempPoint.y);
CGContextAddLineToPoint(context, tempPoint2.x, tempPoint2.y);
}
CGContextStrokePath(context);
}
Run Code Online (Sandbox Code Playgroud)
我在创建用于测试的数组时包含了我的initWithFrame:方法(CGPoint不是对象,因此必须弄清楚如何将它们包含在字典中).
所以这将循环并创建n行.
您可以根据需要重复此位(每次显示不同的坐标).
CGContextMoveToPoint(context, self.startPoint.x, self.startPoint.y);
CGContextAddLineToPoint(context, self.endPoint.x, self.endPoint.y);
Run Code Online (Sandbox Code Playgroud)
第一行代码设置行的起始点,第二行代码绘制它.您无需重复所有其余代码.
换句话说,您可以像这样绘制多条线:
//set up context
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, self.lineWidth1);
CGContextSetStrokeColorWithColor(context, self.lineColor1.CGColor);
//line1
CGContextMoveToPoint(context, self.startPoint1.x, self.startPoint1.y);
CGContextAddLineToPoint(context, self.endPoint1.x, self.endPoint1.y);
//line 2
CGContextMoveToPoint(context, self.startPoint2.x, self.startPoint2.y);
CGContextAddLineToPoint(context, self.endPoint2.x, self.endPoint2.y);
//line 3
etc...
//finished drawing
CGContextStrokePath(context);
Run Code Online (Sandbox Code Playgroud)
每次绘制一条线时都不需要复制整批,你可以重复这两行代码.
| 归档时间: |
|
| 查看次数: |
6550 次 |
| 最近记录: |