尝试为每个笔画设置唯一的 CoreGraphics (iphone) 状态
我有许多“线条”对象——每个对象都存储有关使用 Core Graphics 绘制的线条的信息。 问题是,尽管可能有多个线条对象,每个线条对象都具有唯一的颜色和描边宽度,但所有线条都在相同的颜色和描边宽度下绘制。
每个线条对象都具有描边颜色、描边宽度和 CGPoints 的 NSMutableArray 等属性。 在我的drawRect方法中,我有一个NSEnumerator迭代器来处理每个线对象的每个CGPoint,还有一个while循环,它对我的集合中的每个线对象执行上述操作。 在每条新行的开头,我使用 CGContext 方法设置描边颜色和粗细(代码如下)。 如何用自己独特的颜色绘制每条线?
- (void)drawRect:(CGRect)rect {
if(hasDrawnBefore){
myContext = UIGraphicsGetCurrentContext();
CGContextSetLineCap(myContext, kCGLineCapRound);
int numberOfLines = [myLines count];
int h = 0;
while(h < numberOfLines){
CGContextSetStrokeColorWithColor(myContext, [[[myLines objectAtIndex:h] lineColor] CGColor]);
CGContextSetLineWidth(myContext, [[myLines objectAtIndex:h] lineThickness]);
NSLog(@"<---------------------- Changed Color and Stroke-Width! ------------------------>");
NSEnumerator *myEnumerator = [[[myLines objectAtIndex:h] linePoints] objectEnumerator];
PointObject* object;
int enumIndex = 0;
while ((object = [myEnumerator nextObject])) {
if(enumIndex == 0){
CGContextMoveToPoint(myContext, object.coordX, object.coordY);
}else{
CGContextAddLineToPoint(myContext, object.coordX, object.coordY);
}
enumIndex++;
}
NSLog(@"Just Iterated on the %i th line of %i lines", h, numberOfLines);
h++;
}
CGContextStrokePath(myContext);
NSLog(@"DRAWING");
}else{
NSLog(@"No Drawings Yet");
}
}
I've got a number of 'line' objects -- each storing information about a line drawn using Core Graphics. The problem is, that though there may be several line objects each having a unique color and stroke width, all lines are getting drawn under the SAME color and stroke width.
Each line object has attributes such as stroke color, stroke width, and an NSMutableArray of CGPoints. In my drawRect method, I have an NSEnumerator iterator working through each CGPoint of each line object and a while loop that does the above to each line Object in my collection. At the start of each new line I set the stroke color and thickness using CGContext methods (code below). How can I draw each line with its own unique color?
- (void)drawRect:(CGRect)rect {
if(hasDrawnBefore){
myContext = UIGraphicsGetCurrentContext();
CGContextSetLineCap(myContext, kCGLineCapRound);
int numberOfLines = [myLines count];
int h = 0;
while(h < numberOfLines){
CGContextSetStrokeColorWithColor(myContext, [[[myLines objectAtIndex:h] lineColor] CGColor]);
CGContextSetLineWidth(myContext, [[myLines objectAtIndex:h] lineThickness]);
NSLog(@"<---------------------- Changed Color and Stroke-Width! ------------------------>");
NSEnumerator *myEnumerator = [[[myLines objectAtIndex:h] linePoints] objectEnumerator];
PointObject* object;
int enumIndex = 0;
while ((object = [myEnumerator nextObject])) {
if(enumIndex == 0){
CGContextMoveToPoint(myContext, object.coordX, object.coordY);
}else{
CGContextAddLineToPoint(myContext, object.coordX, object.coordY);
}
enumIndex++;
}
NSLog(@"Just Iterated on the %i th line of %i lines", h, numberOfLines);
h++;
}
CGContextStrokePath(myContext);
NSLog(@"DRAWING");
}else{
NSLog(@"No Drawings Yet");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当您调用 CGContextStrokePath 时,才会实际绘制线条,因此所有线条都会使用您添加的最后一行的颜色和宽度绘制。
我认为如果你只是
在 while 循环内移动该行,你就会得到你想要的行为。
CGContextStrokePath 还会在绘制后清除当前路径,因此应该没问题。
The lines only actually get drawn when you call CGContextStrokePath, so all your lines are getting drawn with the color and width of the last line you added.
I think if you just move the
line inside the while loop, you will get your desired behavior.
CGContextStrokePath also clears the current path after drawing, so it should be fine.