Isn't property layer of UIView be readonly?

余生长醉 提交于 2019-12-23 03:29:10

问题


The property layer of UIView is described in Apple's doc as following:

layer

The view’s Core Animation layer used for rendering. (read-only)

@property(nonatomic, readonly, retain) CALayer *layer

It is obviously that it is readonly. But in my project, why it could be set as following:

NSLog(@"before: %f",self.myView.laye.frame.size.width);
[self.myView.layer setAffineTransform:CGAffineTransformMakeScale(2, 2)];
NSLog(@"after: %f",self.myView.laye.frame.size.width);
//log shows us that the frame is modified 

Really confused in this situation. Anyone can help me out? Thanks in advance!


回答1:


The layer property is read-only, it means you cannot change the layer for another, however the CALayer object contained in the property is not immutable, you can set its own properties.

You cannot do:

self.myView.layer = newLayer;
// equivalent to [self.myView setLayer:newLayer];

But you can do:

[self.myView.layer setAffineTransform:CGAffineTransformMakeScale(2, 2)];

It's the setLayer: selector that you can't use.




回答2:


CALayer is not part of UIKit. It’s part of the Quartz Core framework

while UIView class is a part of UIKit. You can read the documentation of both to know the differences

UIView inherits from NSObject and CALayer also inherits from NSObject so at the time you are doing: [self.myView.layer setAffineTransform:CGAffineTransformMakeScale(2, 2)];

You are not assigning the layer , you are actually accessing the CALayer class layer properties directly and therefore you can play with position, size, and transformation of a layer, as you can see in CALayer documentation, it allows all these things



来源:https://stackoverflow.com/questions/12837077/isnt-property-layer-of-uiview-be-readonly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!