English 中文(简体)
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. How exactly can I do this?

最佳回答

You can look at Core Animation. There is a layer called the CAReplicatorLayer that might help you. Alternatively you can use generic CALayers and set their contents all to the same image. 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. 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.

问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签