iphone - 使用drawRect :?进行自定义渲染时我是否负责裁剪

标签 iphone cocoa-touch

当我在 UIView 上调用 setNeedsDisplayInRectdrawRect: 方法触发时,我是否有责任确保我没有渲染我调用的 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/

相关文章:

iphone - 复制到通用粘贴板

iphone - 使用quartz-2d时如何实现触摸

ios - Swiffy输出透明背景

objective-c - MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification 的原因

ios - 将 UINavigationBar 添加到以模态方式呈现的 TableViewController 中?

ios - 核心数据一对多关系。许多人为所有人出现

iphone从AppDelegate设置RightBarButtonItem

iphone - iTunes Connect 应用程序名称与 Bundle 在 info.plist 中的显示名称

iphone - iOS 通知到达时推送特定 View

iphone - 在使用 AVFoundation 的 Iphone 照片捕获中,图像捕获比闪光灯慢