【问题标题】:Core Graphics drawings incorrectly scaled and translated in device but okay in Interface BuilderCore Graphics 绘图在设备中不正确地缩放和翻译,但在 Interface Builder 中正常
【发布时间】:2014-11-14 21:16:42
【问题描述】:

我正在尝试使用 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.circleColorUIColorIBInspectable 属性,我已将其设置为白色。这是结果,没有问题:

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

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

(底部的黑条其实是我拉黑的标签栏,没有什么异常)

【问题讨论】:

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


    【解决方案1】:

    检查这一行:

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

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

    【讨论】:

    • 谢谢,将frame 更改为bounds 解决了问题。关于为什么它可以解决的任何建议?
    • frame.origin 是您的视图在其父视图中的原点。 bounds.origin (通常)是视图的左上角 (0,0)。见stackoverflow.com/questions/1210047/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2013-09-13
    • 2019-01-22
    • 1970-01-01
    • 2017-10-16
    相关资源
    最近更新 更多