在视图中绘制许多 UIBezierPath
我正在基于手指移动的视图上绘制几个 UIBezierPath
。
每次触摸周期(开始/移动/结束)完成时,我都会存储这些点并创建一个 UIBezierPath
,该路径存储在名为 bezierArray
的数组中。我有另一个名为 bezierArrayColors
的数组,它存储每个路径的颜色。
问题是这样的。该类使用drawRect
。据我所知,每次 drawRect
运行时,它都必须绘制所有创建过的路径,并且应用程序现在很慢。
这是我现在的drawRect
。我知道这很蹩脚,但我不知道如何做到这一点。
- (void)drawRect:(CGRect)rect {
for (int i=0; i<[self.bezierArray count]; i++) {
UIBezierPath *aPath = (UIBezierPath*)[self.bezierArray objectAtIndex:i];
UIColor *aColor = (UIColor*)[self.bezierArrayColor objectAtIndex:i];
[aPath setLineWidth:LINE_WIDTH];
[aColor setStroke];
[aPath stroke];
}
}
有没有办法使用子路径来绘制具有不同颜色或宽度的 UIBezierPath
?我的意思是,改变子路径的颜色、宽度和其他属性?这将允许我将一个 UIBezierPath 与多个不同的子路径一起使用。我希望可以绘制一个贝塞尔曲线并将其留在那里,而无需每次都重新绘制。我缺少什么?
I am drawing several UIBezierPath
s on a view based on finger movements.
Every time a cycle of touches -- Began/Moved/Ended -- completes, I store the points and create a UIBezierPath
that is stored in an array called bezierArray
. I have another array called bezierArrayColors
that stores the colors of each path.
The problem is this. The class uses drawRect
. As far as I can see, every time drawRect
runs, it has to draw all the paths that were ever created and the app is now slow.
This is my drawRect
now. I know it is pretty lame, but I am not seeing how this can be done.
- (void)drawRect:(CGRect)rect {
for (int i=0; i<[self.bezierArray count]; i++) {
UIBezierPath *aPath = (UIBezierPath*)[self.bezierArray objectAtIndex:i];
UIColor *aColor = (UIColor*)[self.bezierArrayColor objectAtIndex:i];
[aPath setLineWidth:LINE_WIDTH];
[aColor setStroke];
[aPath stroke];
}
}
Is there a way to stroke a UIBezierPath
with different colors or perhaps widths using subpaths? I mean, to change color, width and other properties of a subpath? That would allow me to use one UIBezierPath
with several different subpaths. I wish one bezier could be drawn and left there without needing to be redrawn every time. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您注意传递到 -drawRect: 的矩形。如果您的代码采用简单的方法并在每次调用 -drawRect: 时重新绘制整个视图,则至少在某些时候您可能会进行比必要的绘制更多的操作。
Make sure that you pay attention to the rect that's passed into -drawRect:. If your code takes the easy way out and redraws the entire view every time -drawRect: is called, you may be doing far more drawing than necessary at least some of the time.
在单独的子视图中绘制每个贝塞尔曲线路径。这样,每个贝塞尔曲线只有在其本身发生变化时才需要重新绘制。
Draw each bezier path in a separate subview. That way each bezier only has to be redrawn when it itself has changed.
我有类似的问题,并计划使用一个子视图来保存所有“已完成”路径,并使用另一个子视图来仅保存“正在进行”的路径。这样,当我为“进行中”路径获取新的 TouchMoved 事件时,我不必绘制所有已完成的路径。路径完成后,我将其移动到已完成的数组,重新绘制已完成的子视图,然后等待下一次触摸。这避免了“数万亿个子视图”问题,并且还避免了在实际尝试响应对延迟非常敏感的触摸时重绘整个路径数组。一旦我开始这样做,我会尽量记住带着一些代码返回这里。
I have a similar problem and plan to use a subview to hold all the "completed" paths and another subview to hold just the path "in progress". This way I don't have to draw all the completed paths as I get new touchesmoved events for the "in progress" path(s). Once the path is complete, I move it to the completed array, redraw the completed subview, and await the next touch. This avoids the "trillions of subviews" problem, and also avoids redrawing the whole array of paths while actually trying to respond to touches, which are very sensitive to delays. Once I get this going, I'll try to remember to return here with some code.