简体   繁体   English

旋转UIView包括CALayer边界和CALayer帧

[英]Rotating UIView including the CALayer bounds AND the CALayer frame

I want to rotate my "containerView" UIView (semi-transparent red box in the screenshot) using UIDeviceOrientationDidChangeNotification and then later (after it rotates) add sublayers to the view's layer, but I'm finding that only the containerView.layer's bounds are resized, and NOT the containerView.layer's frame! 我想使用UIDeviceOrientationDidChangeNotification旋转我的“containerView”UIView(截图中的半透明红色框),然后稍后(在它旋转之后)将子图层添加到视图的图层,但我发现只调整了containerView.layer的边界大小,而不是containerView.layer的框架! :( :(

Here's how it looks in portrait: 这是它在肖像中的样子:

肖像

Example code: 示例代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)viewDidAppear:(BOOL)animated
{
    CALayer *r = [CALayer layer];
    r.frame = CGRectMake(0, 0, 100, 100);
    r.contents = (id)[[UIImage imageNamed:@"r.png"] CGImage];
    [self.containerView.layer addSublayer:r];
}

- (void)deviceRotated:(NSNotification *)notification
{
    ...
    if (orientation == UIDeviceOrientationLandscapeLeft) {
        self.containerView.transform = CGAffineTransformMakeRotation(RADIANS(90));
        self.containerView.bounds = CGRectMake(0, 0, 411.0, 320.0);

        NSLog(@"container bounds: %@", NSStringFromCGRect(self.containerView.bounds));
        NSLog(@"container layer frame: %@", NSStringFromCGRect(self.containerView.layer.frame));
        NSLog(@"container layer bounds: %@", NSStringFromCGRect(self.containerView.layer.bounds));

        CALayer *testLayer = [CALayer layer];
        testLayer.backgroundColor = [[UIColor blueColor] CGColor];
        testLayer.bounds = self.containerView.layer.frame;
        testLayer.position = CGPointMake(CGRectGetMidX(testLayer.bounds), CGRectGetMidY(testLayer.bounds));
        [self.containerView.layer addSublayer:testLayer];
    }
}

which, after rotating, prints: 旋转后打印:

container bounds: {{0, 0}, {411, 320}}
container layer frame: {{0, 0}, {320, 411}}
container layer bounds: {{0, 0}, {411, 320}}

(Notice the layer's frame is still 320 wide, 411 high.) (注意图层的帧仍然是320宽,411高。)

So now it looks like: 所以现在它看起来像:

景观离开了

I also tried setting the center before changing the bounds, but that looks like: 我也尝试在更改边界之前设置中心,但看起来像:

设置中心留下的景观

I know that the containerView's layer doesn't autoresize, so I tried just setting the layer's frame after the transform... self.containerView.layer.frame = self.containerView.bounds; 我知道containerView的图层没有自动调整大小,所以我尝试在变换后设置图层的框架... self.containerView.layer.frame = self.containerView.bounds; which then prints out: 然后打印出来:

container bounds: {{0, 0}, {320, 411}}
container layer frame: {{0, 0}, {411, 320}}
container layer bounds: {{0, 0}, {320, 411}}

and looks like: 看起来像:

左侧的景观和设置图层框架

Argh! 哎呀!

autoResizesSubviews is set to YES for all views, clipsToBounds is set to NO. 所有视图的autoResizesSubviews都设置为YES,clipsToBounds设置为NO。 I can't use shouldAutorotateToInterfaceOrientation because the real app will have a camera preview so I want the root view to remain in portrait. 我不能使用shouldAutorotateToInterfaceOrientation因为真正的应用程序将有一个相机预览,所以我希望根视图保持纵向。

I must be missing something with how I'm doing the transform on containerView to get its layer's bounds and frame to be the same, but I can't figure out what it is! 我必须错过一些关于我如何在containerView上进行转换以使其图层的边界和框架相同的东西,但我无法弄清楚它是什么! Any help would be much appreciated. 任何帮助将非常感激。

Update - Fixed: 更新 - 修正:

Setting the newly added layer's bounds ( testLayer.bounds ) to contentView.layer.bounds INSTEAD of contentView.layer.frame did the trick. 设置新添加图层边界( testLayer.bounds )到contentView.layer.bounds代替contentView.layer.frame的伎俩。 (Since transforming the contentView invalidates its layer.frame.) Thanks Michael! (因为转换contentView会使其layer.frame失效。)谢谢迈克尔!

Fixed: 固定:

contentView.layer.bounds而不是contentView.layer.frame

  self.containerView.bounds = CGRectMake(0, 0, 411.0, 320.0);

Your setting here is wrong because of the flipped context. 由于翻转的上下文,您在此处的设置是错误的。 You set it to be wide and because it is flipped 90 degrees it goes back to being vertical. 您将其设置为宽,因为它翻转90度后会回到垂直状态。

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

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