我在一个名为containerView的UIView
上创建了一个跨越整个屏幕的视图。在这个视图中,必须添加一个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
但两者都不重叠。这是屏幕截图。
注意:我正在横向工作。
答案 0 :(得分: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
的第一个参数。
答案 1 :(得分:0)
我可以搞清楚我的错误。 topLayer的配置有问题。这应该适合doubleSidedLayer
的范围。所以我将[topLayer setFrame:doubleLayerFrame]
更改为[topLayer setFrame:doubleSidedLayer.bounds]
。这解决了我的问题。