CALayer 未类型转换为 CATiledLayer

发布于 2024-12-16 18:05:45 字数 548 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月流歌 2024-12-23 18:05:45

在 Objective-C 中对指针应用类型转换在运行时没有效果(它在编译时确实有一些效果,所以有时仍然使用这种语法)。

所以,你的“类型转换”实际上并不是在进行类型转换。它只是通知编译器(仅在那一行代码中)这是一个 CATiledLayer,而不是声明的 CALayer。实际的对象实际上仍然是一个CATiledLayer

为了更改 self.layer 的类,您定义一个静态方法来返回该类:

@implementation MyView

+ (Class)layerClass
{
  return [CATiledLayer class];
}

@end

现在,当创建任何 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 a CALayer as it has been declared. The actual object is really still a CATiledLayer.

In order to change the class of self.layer, you define a static method to return the class:

@implementation MyView

+ (Class)layerClass
{
  return [CATiledLayer class];
}

@end

And now, when the layer for any MyView instance is created, it will be a CATiledLayer instead of a CALayer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文