如何在一个视图中添加的UIView子视图像3x3的表格排列 [英] How to add UIView subviews in a view aligned like 3x3 table

查看:448
本文介绍了如何在一个视图中添加的UIView子视图像3x3的表格排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个主视图,并在这我添加9 UIMyView,我从UIView的子类。 我想补充的是实例UIMyViews使用一个NSBundle加载笔尖文件和addSubview在主视图控制器,当然所有这些观点都出现了超景一隐藏另一个,最后一个可见的左上角,我需要在超视图使用点莫名其妙地定位,或创造这样一个表格,并添加到其细胞UIMyViews它要么?!

I have created a main View and in that I add 9 UIMyView that I subclassed from UIView. I add instances of that UIMyViews using NSBundle to load the nib file and addSubview in the main view controller, of course all of these views are appeared in the upper left corner of the super view one hiding the other and the last one to be visible, which either I need to position them somehow using points in super view or create something like a table and add to its cells the UIMyViews?!

假设你有一个表,把9台卡放置3行3列(9卡)。 这就是我需要实现。

Imagine you have a table and put 9 deck cards positioned 3 rows and 3 columns (9 cards). This is what I need to achieve.

选中该图片:的http://www.wpclipart.com/recreation/games/card_deck/cards_symbols/playing_card_symbols.png

我只需要3行3列。

任何人都可以建议的最佳实践为这种操作?

Anyone can suggest best practice for this kind of operation?

后来才在心中有我想要实现这些UIMyViews各种动画和特效,同时draged,感动和重新排列等。

Afterwards just to have in mind I want to implement various animations and effects on these UIMyViews while draged, touched and reordering etc.

感谢你。

推荐答案

如果您通过所有的意见要循环和他们在一个网格位置,可以使这样的:

If you want to loop through all the views and position them in a grid, you can so like this:

    // Array of 9 views    
NSArray *views = [NSArray arrayWithObjects:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)], nil];

int count = 0;
int maxPerRow = 3;
int row = 0;
float spacing = 100.0;

for(UIView *view in views)
{
    // Only added to tell the difference between views, make sure to import QuartzCore or remove the next 3 lines
    view.backgroundColor = [UIColor colorWithRed:.5 green:0 blue:0 alpha:1];
    view.layer.borderWidth = 1;
    view.layer.borderColor = [UIColor whiteColor].CGColor;

    // position view
    view.frame = CGRectMake(count*spacing, row * spacing, view.frame.size.width, view.frame.size.height);
    [self.view addSubview:view];

    if(count % maxPerRow == maxPerRow-1)
    {
        count = 0;
        row++;
    }
    else
    {
        count++;
    }
}

这会给你是这样的:

这篇关于如何在一个视图中添加的UIView子视图像3x3的表格排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆