核心图形绘图在设备中缩放和翻译不正确,但在Interface Builder中没问题

时间:2014-11-14 21:16:42

标签: xcode interface-builder core-graphics xcode6 cgcontext

我正在尝试使用Core Graphics绘制一些弧和圆。我正在使用新的Interface Builder功能IBDesignable / IBInspectable来实时查看我的视图,它只是在Interface Builder中发挥作用。非常简单地说,这是我绘制圆圈的代码:

-(void)drawCircleInRect:(CGRect)rect{
    [self.circleColor setFill];
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.frame];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);

    // If you have content to draw after the shape,
    // save the current state before changing the transform.
    CGContextSaveGState(context);

    [path fill];

    // Restore the graphics state before drawing any other content.
    CGContextRestoreGState(context);
}

我在drawRect:中调用此内容,而self.circleColorIBInspectable的{​​{1}}属性,我将其设置为白色。这是结果,没有问题:

enter image description here

然而,当我在模拟器或设备上运行时(尝试两者,结果是相同的),这就是我得到的:

enter image description here

它就像在视图中缩放/错误定位一样,因此被剪裁。为什么会这样?这就像是2倍。如果反之亦然,我认为它与Retina比例因子有关,但这恰恰相反。它在视网膜设备中绘制得太大,并且在我的非视网膜Mac的Interface Builder上绘制正常。有什么建议吗?

(底部黑条实际上是一个标签栏,我已经涂黑了,那里没有任何异常)

1 个答案:

答案 0 :(得分:1)

检查此行:

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.frame];

应该是self.bounds,而不是self.frame