简体   繁体   English

iPhone SDK:重复子视图

[英]iPhone SDK: repeat subviews

I have one UIView, which I'm using as my main view, and I want to repeat the subview across the screen. 我有一个UIView,我将其用作主视图,并且想在整个屏幕上重复该子视图。 How exactly can I do this? 我到底该怎么做?

You can look at Core Animation. 您可以看一下Core Animation。 There is a layer called the CAReplicatorLayer that might help you. 有一个称为CAReplicatorLayer的层可能会对您有所帮助。 Alternatively you can use generic CALayers and set their contents all to the same image. 或者,您可以使用通用CALayers并将其内容全部设置为同一图像。 You would just need to figure out the width of your parent view and how big you want each tile to be and then just create CALayers for each tile shifting the position of each new layer depending on your grid dimensions. 您只需要弄清楚父视图的宽度以及每个图块的大小,然后为每个图块创建CALayers,即可根据网格尺寸移动每个新层的位置。 Something like this: 像这样:

UIImage *imageToReplicate = [UImage imageNamed:@"tile"];
for (i = 0; i < 10; ++i)
{
    for (j=0; j < 10; ++j)
    {
        CGFloat xPos = 0.0; // Calculate your x position
        CGFloat yPos = 0.0; // Calculate your y position

        CALayer *layer = [CALayer layer];
        [layer setBounds:CGRectMake(0.0f, 0.0f, TILE_WIDTH, TILE_HEIGHT)];
        [layer setPosition:CGPointMake(xPos, yPos)];
        [layer setContents:(id)[image CGImage]];
        [[[self view] layer] addSublayer:layer];
    }
}

You'll have to figure out the calculation for each iteration of your layer positions. 您必须找出图层位置每次迭代的计算。 Remember that by default the anchor point of the layer is its center. 请记住,默认情况下,图层的锚点是其中心。 You either calculate it by subtracting half of the layer tile size or you can change the anchor point to be a corner instead. 您可以通过减去一半图层图块大小来计算它,也可以将锚点更改为角。 For more information on that, take a look at the layer geometry section of the Core Animation documentation . 有关更多信息,请参阅Core Animation文档图层几何部分

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

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