CALayer 未类型转换为 CATiledLayer
我是 iPhone 编程新手。我想使用 CATiledLayer 加载图像。 我正在创建 UIVIew 类的子类,并在该类的 init 方法中编写,
CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
但是当我编写时
tiledLayer.levelsOfDetail = 4;
它给了我错误: [CALayer setLevelsOfDetail:]: 无法识别的选择器发送到实例 0xcd04450
因此,为了检查 tiledLayer 的类类型,我正在编写以下语句:
NSString *pqr = [[NSString alloc]initWithFormat:@"%@", [tiledLayer class]];
NSLog(pqr);
但它打印 CALayer 而不是 CATiledLayer。为什么会这样??我缺少什么? 现在我被困在这里了。 :(
I am new to iPhone programming. I want to use CATiledLayer to load an image.
I am creating the subclass of UIVIew class and in the init method of that class, I am writing,
CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
But when I am writing
tiledLayer.levelsOfDetail = 4;
It gives me the error that
[CALayer setLevelsOfDetail:]: unrecognized selector sent to instance 0xcd04450
So to check the class-type of tiledLayer,I am writing following statements :
NSString *pqr = [[NSString alloc]initWithFormat:@"%@", [tiledLayer class]];
NSLog(pqr);
But it prints CALayer instead of CATiledLayer. Why is it so?? What am I missing??
Now I am stuck here. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Objective-C 中对指针应用类型转换在运行时没有效果(它在编译时确实有一些效果,所以有时仍然使用这种语法)。
所以,你的“类型转换”实际上并不是在进行类型转换。它只是通知编译器(仅在那一行代码中)这是一个
CATiledLayer
,而不是声明的CALayer
。实际的对象实际上仍然是一个CATiledLayer
。为了更改
self.layer
的类,您定义一个静态方法来返回该类:现在,当创建任何
MyView
实例的图层时,它将是CATiledLayer
而不是CALayer
。Applying a typecast to a pointer in Objective-C has no effect at runtime (it does have some effect during compile time, so this syntax is still used at times).
So, your "typecast" is not actually doing a typecast. It's just informing the compiler (in that one line of code only) that this is a
CATiledLayer
and not aCALayer
as it has been declared. The actual object is really still aCATiledLayer
.In order to change the class of
self.layer
, you define a static method to return the class:And now, when the layer for any
MyView
instance is created, it will be aCATiledLayer
instead of aCALayer
.