简体   繁体   English

CALayer在设备方向更改时在iOS中自动调整大小

[英]CALayer auto resizing in iOS on device orientation change

I have added one more CALayer for Custom View ,The code is 我为自定义视图添加了一个CALayer,代码是

hostedGraphLayer.frame = self.layer.bounds;
[self.layer addSublayer:hostedGraphLayer];

When device orientation changes view's layer's sublayer also rotated but it is not autoresizing with respect to view. 当设备方向改变时,视图的图层的子图层也会旋转,但它不会相对于视图自动调整大小。 Layer wont have autoresize property for iOS. Layer不具有iOS的autoresize属性。

One of the answer already in stackOverFlow is CALayers didn't get resized on its UIView's bounds change. stackOverFlow中已有的答案之一是CALayers没有根据其UIView的边界变化调整大小。 Why? 为什么? In this case setting layer's frame on device orientation change ,layer wont autoresize with respect to its view in between rotation start and end, this can be seen clearly in simulator by toggling slow animation. 在这种情况下设置图层在设备方向上的框架更改,图层不会相对于其在旋转开始和结束之间的视图自动调整,这可以通过切换慢动画在模拟器中清楚地看到。

Is there any fix for this "When view is rotated its layers also get rotated and it should resize wrt its view". 是否有任何修复此“当视图旋转时,它的图层也会旋转,它应该调整其视图”。

No there is not built in fix for that Yet, 没有没有内置修复但是,

You have to change its frames . 你必须改变它的框架。

correct me if i am wrong. 如果我错了,请纠正我。

In the view attributes inspector, set the view mode to redraw. 在视图属性检查器中,将视图模式设置为重绘。
查看属性检查器
The content will no longer adapt or align or even stretch, but will render along the animation. 内容将不再适应或对齐甚至拉伸,而是沿着动画呈现。

What truly happens is it renders to the final position, and that render is stretched during the animation, but that's a trick pretty much invisible for the user :) 真正发生的是它渲染到最终位置,并且渲染在动画期间被拉伸,但这对于用户来说几乎是不可见的:)

The trick above will call setNeedsDisplay on the background layer ( view.layer ) on view's frame change, but it's up to you to update its children . 上面的技巧将在视图的帧更改上调用背景图层( view.layer )上的setNeedsDisplay但是由您来更新其子项 setNeedsDisplay a layer will not propagate to the sublayers. setNeedsDisplay图层不会传播到子图层。
Also those sublayers will not autoresize like the views. 这些子图层也不会像视图一样自动调整大小。
If you really want that behavior, use views instead, or force redraw yourself by overriding the setNeedsDisplay method of the parent layer. 如果您确实需要该行为,请改为使用视图,或者通过覆盖父图层的setNeedsDisplay方法强制重绘自己。

-(void)setNeedsDisplay
{
    [super setNeedsDisplay];
    for (CALayer *sublayer in self.sublayers) {
        [sublayer setNeedsDisplay];
    }
}

It all depends of what/how you need to redraw. 这完全取决于您需要重绘的内容/方式。

If you really want to use a layer as a background layer which matches the frame of your view, subclass UIView and override its class method +(Class)layerClass to return your layer class. 如果您确实想要使用图层作为与视图框架匹配的背景图层,则子类UIView并覆盖其类方法+(Class)layerClass以返回图层类。

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

It will use your custom layer as default background layer 它将使用您的自定义图层作为默认背景图层
It's very powerful ;) I use it everywhere 它非常强大;)我到处使用它
Don't forget the Redraw attribute ! 不要忘记Redraw属性!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM