Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我如何让我的UIView使用我的自定义CALayer作为其背景层?_Ios_Core Animation - Fatal编程技术网

Ios 我如何让我的UIView使用我的自定义CALayer作为其背景层?

Ios 我如何让我的UIView使用我的自定义CALayer作为其背景层?,ios,core-animation,Ios,Core Animation,我覆盖了苹果文档中推荐的UIView的layerClass方法,如下所示: + (Class)layerClass { return [ViewLayerBase class]; } - (id)initWithRect:(CGRect)rect { if ((self = [super init])) { NSLog(@"I got called"); } return self; } 在我的viewLayerBase自定义类中,我

我覆盖了苹果文档中推荐的UIView的layerClass方法,如下所示:

+ (Class)layerClass {
     return [ViewLayerBase class];
}
- (id)initWithRect:(CGRect)rect {
     if ((self = [super init])) {
         NSLog(@"I got called");
     }
     return self;
}
在我的viewLayerBase自定义类中,我实现了一个init方法,如下所示:

+ (Class)layerClass {
     return [ViewLayerBase class];
}
- (id)initWithRect:(CGRect)rect {
     if ((self = [super init])) {
         NSLog(@"I got called");
     }
     return self;
}
我的UIView子类显然没有调用此方法。考虑到UIView的layer属性是只读的,我不能用rect进行初始化…如何让UIView实例化我的自定义CALayer子类?

你做得对(在自定义UIView类中添加
layerClass
类方法)


问题在于您的init方法。对于CALayer,指定的init方法是init,而不是initWithRect。尝试将代码移动到
-init
。那应该行得通。

行得通。但是,如果我希望我的Layer Init方法与我的视图具有相同的边界,该怎么办?这是自动发生的。当您在类似视图的边界上设置属性时,实际上只是在层上设置属性的一个过程。