简体   繁体   English

在UIView外面画画

[英]Drawing outside a UIView

I have a UIView where I would like to draw a Circle that extends past the frame of the UIView, I have set the masksToBounds to NO - expecting that I can draw past outside the bounds of the UIView by 5 pixels on the right and bottom. 我有一个UIView,我想绘制一个延伸超过UIView框架的圆圈,我已将masksToBounds设置为NO - 期望我可以在UIView的边界之外向右和向下绘制5个像素。

I expect the oval to not get clipped but it does get clipped and does not draw outside the bounds? 我希望椭圆不会被剪裁但是它会被修剪掉并且不会超出边界?

- (void)drawRect:(CGRect)rect
{

    int width = self.bounds.size.width;
    int height = self.bounds.size.height;
    self.layer.masksToBounds = NO;

    //// Rounded Rectangle Drawing
    //// Oval Drawing
    UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(0, 0, width+5, height+5)];
    [[UIColor magentaColor] setFill];
    [ovalPath fill];
    [[UIColor blackColor] setStroke];
    ovalPath.lineWidth = 1;
    [ovalPath stroke];

}

在此输入图像描述

From http://developer.apple.com/library/ios/#documentation/general/conceptual/Devpedia-CocoaApp/DrawingModel.html 来自http://developer.apple.com/library/ios/#documentation/general/conceptual/Devpedia-CocoaApp/DrawingModel.html

UIView and NSView automatically configure the drawing environment of a view before its drawRect: method is invoked. 在调用drawRect:方法之前,UIView和NSView会自动配置视图的绘图环境。 (In the AppKit framework, configuring the drawing environment is called locking focus.) As part of this configuration, the view class creates a graphics context for the current drawing environment. (在AppKit框架中,配置绘图环境称为锁定焦点。)作为此配置的一部分,视图类为当前绘图环境创建图形上下文。

This graphics context is a Quartz object (CGContext) that contains information the drawing system requires, such as the colors to apply, the drawing mode (stroke or fill), line width and style information, font information, and compositing options. 此图形上下文是Quartz对象(CGContext),其中包含绘图系统所需的信息,例如要应用的颜色,绘图模式(笔触或填充),线宽和样式信息,字体信息和合成选项。 (In the AppKit, an object of the NSGraphicsContext class wraps a CGContext object.) A graphics context object is associated with a window, bitmap, PDF file, or other output device and maintains information about the current state of the drawing environment for that entity. (在AppKit中,NSGraphicsContext类的对象包装CGContext对象。)图形上下文对象与窗口,位图,PDF文件或其他输出设备相关联,并维护有关该实体的绘图环境的当前状态的信息。 A view draws using a graphics context associated with the view's window. 视图使用与视图窗口关联的图形上下文绘制。 For a view, the graphics context sets the default clipping region to coincide with the view's bounds and puts the default drawing origin at the origin of a view's boundaries. 对于视图,图形上下文将默认剪切区域设置为与视图的边界重合,并将默认绘图原点放在视图边界的原点。

Once the clipping region is set, you can only make it smaller. 设置剪切区域后,您只能将其缩小。 So, what you're trying to do isn't possible in a UIView drawRect:. 所以,在UIView drawRect中,你想要做的事情是不可能的:

I'm not certain this will fix your problem, but it's something to look into. 我不确定这会解决你的问题,但这是值得研究的问题。 You're setting self.layer.masksToBounds = NO every single time you enter drawRect . 您每次输入drawRect时都设置self.layer.masksToBounds = NO You should try setting it inside the init method just once instead, A) because it's unnecessary to do it multiple times and B) because maybe there's a problem with setting it after drawRect has already been called--who knows. 你应该尝试在init方法中只设置一次,A)因为没有必要多次这样做而且B)因为在调整drawRect之后设置它可能有问题 - 谁知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM