cgcontext drawing - 显示第一组形状,第二组完全相同的设置,稍微向右移动未显示

时间:2015-02-25 12:36:25

标签: ios objective-c

我有以下代码:

- (void)drawRect:(CGRect)rect {

CGContextRef context = UIGraphicsGetCurrentContext();

// Left frame.
CGContextAddRect(context, CGRectMake(0, 0,
                                 frameWidth,
                                 self.frameHeight));
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:frameBorderColorRed
                                                        green:frameBorderColorGreen
                                                         blue:frameBorderColorBlue
                                                        alpha:frameBorderColorAlpha].CGColor);
CGContextFillPath(context);
CGContextAddRect(context, CGRectMake(frameBorderWidth,
                                     frameBorderWidth,
                                     frameWidth - (2*frameBorderWidth),
                                     self.frameHeight - (2*frameBorderWidth)));
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextFillPath(context);

// Right frame.
CGContextAddRect(context, CGRectMake(frameWidth + distanceBetweenFrames,
                                     0,
                                     frameWidth,
                                     self.frameHeight));
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:frameBorderColorRed
                                                        green:frameBorderColorGreen
                                                         blue:frameBorderColorBlue
                                                        alpha:frameBorderColorAlpha].CGColor);
CGContextFillPath(context);
CGContextAddRect(context, CGRectMake(frameWidth + distanceBetweenFrames + frameBorderWidth,
                                     frameBorderWidth,
                                     frameWidth - (2*frameBorderWidth),
                                     self.frameHeight - (2*frameBorderWidth)));
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextFillPath(context);



}

左框显示但右框不显示。但是,如果我要将左框架注释掉,则会显示右框架。我也尝试在文档中绘制线条:

https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGContext/index.html#//apple_ref/c/func/CGContextAddRect

无济于事。

1 个答案:

答案 0 :(得分:0)

我会考虑使用CoreAnimation,因为它使用GPU。 除了问题是您通过以下方式关闭当前路径:

CGContextFillPath(context);

然后尝试使用:

CGContextAddRect

正如文档所说:“添加一条到当前路径的矩形路径。”

您需要使用以下方法添加其他路径:

CGContextMoveToPoint

您可以在此处阅读更多详细信息: https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGContext/index.html