1

我在一个UIView命名的 containerView 上创建了一个跨越整个屏幕的容器视图。在这个视图CATransormLayer上,必须添加一个具有以下尺寸的 UIView。

CATransformlayer : doubleSidedLayer  
Height = containerView.frame.size.height  
Width = containerView.frame.size.width  

UIView : stripView
Height = containerView.frame.size.height  
Width = 20;

它们的 x 位置由变量 positionX 指定。理论上两者都DoubleSidedLayer必须stripView重叠。但他们不是。

这是代码。

CGRect containerFrame =CGRectMake(0,0,self.view.frame.size.height,self.view.frame.size.width);
NSLog(@"container frame = %f %f %f %f",containerFrame.origin.x,containerFrame.origin.y,containerFrame.size.width,containerFrame.size.height);
UIView *containerView = [[UIView alloc]initWithFrame:containerFrame];
[containerView setBackgroundColor:[UIColor grayColor]];

float positionX = containerFrame.size.width/4;//This is the position where the stripView and doubleSidedLayer must be placed

//Configuration of CATransformLayer.
CGRect doubleLayerFrame = CGRectMake(positionX,0,containerFrame.size.width/2,containerFrame.size.height);
CATransformLayer *doubleSidedLayer  = [CATransformLayer layer];
[doubleSidedLayer setFrame:doubleLayerFrame];

NSLog(@"DoubleSidedLayer frame = %f %f %f %f",doubleSidedLayer.frame.origin.x,doubleSidedLayer.frame.origin.y,doubleSidedLayer.frame.size.width,doubleSidedLayer.frame.size.height);
NSLog(@"DoubleSidedLayer bounds = %f %f %f %f",doubleSidedLayer.bounds.origin.x,doubleSidedLayer.bounds.origin.y,doubleSidedLayer.bounds.size.width,doubleSidedLayer.bounds.size.height);
NSLog(@"Anchor Points = %f %f",doubleSidedLayer.anchorPoint.x, doubleSidedLayer.anchorPoint.y);
NSLog(@"position = %f %f",doubleSidedLayer.position.x,doubleSidedLayer.position.y);

//Configuring top layer
CALayer *topLayer = [[CALayer alloc]init];
[topLayer setFrame:doubleLayerFrame];
[topLayer setContents:(id)[UIImage imageNamed:@"1.png"].CGImage];
[topLayer setZPosition:2];
[topLayer setDoubleSided:NO];

//Adding the layers
[doubleSidedLayer addSublayer:topLayer];
[containerView.layer addSublayer:doubleSidedLayer];

//Adding the stripView
UIView *stripView = [[UIView alloc]initWithFrame:CGRectMake(positionX, 0, 20,containerView.frame.size.height)];
[stripView setBackgroundColor:[UIColor cyanColor]];
[containerView addSubview:stripView];
NSLog(@"Strip frame = %f %f %f %f",stripView.frame.origin.x,stripView.frame.origin.y,stripView.frame.size.width,stripView.frame.size.height);

//Adding the containerView to the main view
[self.view addSubview:containerView];

从日志语句中可以看到 doubleSidedLayer 和 stripView 的帧。

doubleSidedLayer frame = 120.000000 0.000000 240.000000 300.000000   
strip frame = 120.000000 0.000000 20.000000 300.000000

但两者不重叠。这是屏幕截图。在此处输入图像描述

注意:我正在横向工作。

4

2 回答 2

0

我想知道是否有更简单的方法来实现您正在寻找的东西。例如,如果我有一些想要将图像从一个图像“翻转”到另一个图像的视图,我会使用如下内容:

[UIView transitionWithView:self.imageView
                  duration:0.5
                   options:UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionCurveEaseInOut
                animations:^{
                    if (self.flipped)
                        self.imageView.image = [UIImage imageNamed:@"front.png"];
                    else
                        self.imageView.image = [UIImage imageNamed:@"back.png"];

                    self.flipped = !self.flipped;

                }
                completion:nil];

我正在使用 imageview 执行此操作,但是对于UIView您使用的任何控件,这个概念都是一样的。如果你有一些容器视图并且你想在这个animations块中添加/删除或显示/隐藏控件,你也可以这样做。只需使用容器视图作为transitionWithView.

于 2013-03-06T14:53:01.830 回答
0

我可以弄清楚我的错误。topLayer 的配置有问题。这应该在 的范围内doubleSidedLayer。所以我[topLayer setFrame:doubleLayerFrame]改为[topLayer setFrame:doubleSidedLayer.bounds]. 这解决了我的问题。

于 2013-03-11T06:54:07.010 回答