首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何正确定位CATransformLayer在其上层的位置

如何正确定位CATransformLayer在其上层的位置
EN

Stack Overflow用户
提问于 2013-03-01 18:45:16
回答 2查看 388关注 0票数 1

我已经在一个名为containerView的UIView上创建了一个跨越整个screen.On的视图,必须添加一个CATransormLayer和一个UIView,它们具有以下维度。

代码语言:javascript
复制
CATransformlayer : doubleSidedLayer  
Height = containerView.frame.size.height  
Width = containerView.frame.size.width  

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

它们的x位置都是由变量positionX指定的。理论上,DoubleSidedLayerstripView都必须重叠。但事实并非如此。

下面是代码。

代码语言:javascript
复制
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的框架。

代码语言:javascript
复制
doubleSidedLayer frame = 120.000000 0.000000 240.000000 300.000000   
strip frame = 120.000000 0.000000 20.000000 300.000000

但这两者并不是重叠的。这是屏幕截图。

注意:我的工作方向是横向。

EN

回答 2

Stack Overflow用户

发布于 2013-03-06 22:53:02

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

代码语言:javascript
复制
[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];

我是用一个图像视图来做这件事的,但是这个概念对你使用的任何UIView控件都是一样的。如果你有一些容器视图,并且你想在这个animations块中添加/删除或者显示/隐藏控件,你也可以这样做。只需使用容器视图作为transitionWithView的第一个参数。

票数 0
EN

Stack Overflow用户

发布于 2013-03-11 14:54:07

我可以找出我的错误。topLayer的配置出现问题。这应该在doubleSidedLayer的范围内。所以我把[topLayer setFrame:doubleLayerFrame]改成了[topLayer setFrame:doubleSidedLayer.bounds]。这解决了我的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15155838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档