Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.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
Iphone 旋转UIView,包括CALayer边界和CALayer框架_Iphone_Cocoa Touch_Uiview_Core Animation_Calayer - Fatal编程技术网

Iphone 旋转UIView,包括CALayer边界和CALayer框架

Iphone 旋转UIView,包括CALayer边界和CALayer框架,iphone,cocoa-touch,uiview,core-animation,calayer,Iphone,Cocoa Touch,Uiview,Core Animation,Calayer,我想使用UIDeviceOrientationIDChangeNotification旋转我的“containerView”UIView(屏幕截图中的半透明红色框),然后稍后(旋转后)将子层添加到视图的层中,但我发现只有containerView.layer的边界被调整大小,而不是containerView.layer的框架!:( 下面是它在肖像中的外观: 示例代码: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrie

我想使用
UIDeviceOrientationIDChangeNotification
旋转我的“containerView”UIView(屏幕截图中的半透明红色框),然后稍后(旋转后)将子层添加到视图的层中,但我发现只有containerView.layer的边界被调整大小,而不是containerView.layer的框架!:(

下面是它在肖像中的外观:

示例代码:

- (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];
    }
}
旋转后,打印:

container bounds: {{0, 0}, {411, 320}}
container layer frame: {{0, 0}, {320, 411}}
container layer bounds: {{0, 0}, {411, 320}}
看起来:

AutoResizesSubView对于所有视图都设置为“是”,clipsToBounds设置为“否”。我不能使用
shouldAutorotateToInterfaceOrientation
,因为真实应用程序将具有相机预览,所以我希望根视图保持纵向

我在containerView上进行转换以使其层的边界和帧保持一致,但我不知道它是什么!如果有任何帮助,我将不胜感激

更新-修复:

将新添加的层边界(
testLayer.bounds
)设置为
contentView.layer.bounds
,而不是
contentView.layer.frame
),成功了。(因为转换contentView会使其层框架失效。)谢谢Michael

固定的:

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


由于翻转的上下文,您在此处的设置是错误的。您将其设置为宽,并且由于翻转90度,它会恢复为垂直状态。

在我以anwser身份发布之前,您是否尝试翻转self.containerView.bounds=CGRectMake(0,0,411.0,320.0)中的值;谢谢你的建议!我试过了,看起来差不多了:-虽然容器层框架是{-45.5,45.5},{411320},层的边界是:{{0,0},{320411}转换视图时,帧值不可用。来自apple doc警告:如果转换属性不是标识转换,则此(帧)的值属性未定义,因此应忽略。请注意,在添加之前,您应该检查
viewdideappear
中已经存在的层,否则您可能会多次添加该层。例如,每当您将另一个视图控制器推到堆栈上并释放它时,
viewdideappear
wil我又被呼叫了。谢谢,唯一的问题是现在containerView的边界看起来仍然是320 x 411。如果我强制它们为411 x 320,我就回到原点,层帧恢复为320 x 411(胜利!!因为转换后,containerView.layer的框架未定义/被忽略,我将
testLayer.bounds=self.containerView.layer.frame;
行更改为
testLayer.bounds=self.containerView.layer.bounds;
,这很有效。-非常感谢!没问题,很高兴我能提供帮助,是的,只要转换发生frame属性变得很垃圾。你知道,我也曾在文档中读到过,但我完全忘记了。再次感谢。
  self.containerView.bounds = CGRectMake(0, 0, 411.0, 320.0);