したがって、2つの異なる線を描く必要があります。別のpostingを通して、私は行を再描画する方法を考え出しました。私の質問は、私は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);
}
実装
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];
これはただの行を再描画します。私はそれぞれの行のdrawRectブロックを繰り返しますか?だから私は2行が必要な場合、私はする必要がありますか:
- (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);
}
または私の行を描画するより良い方法はありますか?
EDIT:最終的な目標は、私が形式として使用していたラベルとテキストボックスとUIView
でそれを使用することです。私はラインがフォームを分割したい。
私はちょうどこれらのカスタムを使用する必要がある場合、私は思ったんだけどだけラインとしてUIView
秒draw2Dと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);
}
私は私のinitWithFrameが含まれていました。私はテストのために使用される配列を作成する方法の方法(するCGPointのがオブジェクトではありません、それらを辞書に含める方法を理解しなければならなかった)。
ので、これがループを通ってn行を作成します。
FYIでは、 "for(NSDictionary * dict in self.pointArray){..."を使用してループを改善することができます。次に、iで配列内のオブジェクトを毎回ルックアップする必要はありません。この種のループは「高速反復」として知られており、配列や辞書内のあらゆる種類のオブジェクトで実行できます。 –
興味深いことに、私は前に(idキーのNSDictionaryで)使用しましたが、これを見たことがない、私はそれを調べます。ありがとう。 – Padin215