当我在 UIView
上调用 setNeedsDisplayInRect
且 drawRect:
方法触发时,我是否有责任确保我没有渲染我调用的 CGRect 之外的内容,或者框架会为我处理这些内容吗?
示例:
-(void)drawRect:(CGRect)rect
{
//assume this is called through someMethod below
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor redColor] setFill];
CGContextFillRect(ctx, rect);
[[UIColor blueColor] setFill];
// is this following line a no-op? or should I check to make sure the rect
// I am making is contained within the rect that is passed in?
CGContextFillRect(ctx, CGRectMake(100.0f, 100.0f, 25.0f, 25.0f));
}
-(void)someMethod
{
[self setNeedsDisplayInRect:CGRectMake(50.0f, 50.0f, 25.0f, 25.0f)];
}
最佳答案
为了简化巴里所说的:是的,框架会为你处理它。
您可以安全地忽略该矩形,您在其之外绘制的任何内容都将被忽略。
另一方面,如果您在矩形之外进行绘制,则会浪费 CPU 时间,因此如果您可以基于矩形限制您的绘制,那么您应该这么做。
关于iphone - 使用drawRect :?进行自定义渲染时我是否负责裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/583391/