尝试为每个笔画设置唯一的 CoreGraphics (iphone) 状态

发布于 2024-08-01 20:16:13 字数 1712 浏览 6 评论 0原文

我有许多“线条”对象——每个对象都存储有关使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2024-08-08 20:16:13

仅当您调用 CGContextStrokePath 时,才会实际绘制线条,因此所有线条都会使用您添加的最后一行的颜色和宽度绘制。
我认为如果你只是

CGContextStrokePath(myContext);

在 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

CGContextStrokePath(myContext);

line inside the while loop, you will get your desired behavior.
CGContextStrokePath also clears the current path after drawing, so it should be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文