【发布时间】:2014-09-25 14:55:21
【问题描述】:
是否可以创建一个在 Xcode 中实时呈现的UIView 子类(通过添加here 解释的IB_DESIGNABLE 属性)但没有自定义drawRect: 方法?
我们有一个自定义的UIView 子类,它使用一些CAShapeLayers 添加到self.layer 进行绘图(因此,无需覆盖drawRect:)。此类在 App 上运行良好,但不会在 Xcode 上呈现。
如果我们复制drawRect: 中的代码,它可以工作,但我们希望让绘图在图层上自动进行。
这个可以吗?
我也试过了
- (void)drawRect:(CGRect)rect
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(currentContext, self.myLayer1.frame.origin.x, self.myLayer1.frame.origin.y);
[self.myLayer1 renderInContext:currentContext];
CGContextMoveToPoint(currentContext, self.myLayer1.frame.origin.x, self.myLayer1.frame.origin.y);
[self.myLayer2 renderInContext:currentContext];
}
这似乎适用于设备,但不适用于 Xcode 的 IB。
【问题讨论】:
-
确保您的初始化(即设置
CAShapeLayers)代码也从initWithFrame:调用。 IB 称之为预览。 -
是的,我也从那里调用它。没运气。你怎么知道IB使用
initWithFrame:? IB 预览时是否有调试视图代码的方法? -
Editor>Debug Selected Views来自 Xcode 菜单。 -
Debug Selected Views 使 Xcode 在我的自定义视图中崩溃。 :-(
-
我将崩溃和 CALayer 实时渲染问题作为错误报告提交给 Apple。
标签: ios xcode uiview interface-builder ibdesignable