如何在一个视图中添加的UIView子视图像3x3的表格排列视图、排列、表格、如何在

由网友(没了对象省了流量)分享简介:我创建了一个主视图,并在这我添加9 UIMyView,我从UIView的子类。我想补充的是实例UIMyViews使用一个NSBundle加载笔尖文件和addSubview在主视图控制器,当然所有这些观点都出现了超景一隐藏另一个,最后一个可见的左上角,我需要在超视图使用点莫名其妙地定位,或创造这样一个表格,并添加到其细...

我创建了一个主视图,并在这我添加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.

选中该图片:的https://p.xsw88.cn/allimgs/daicuo/20230911/3422.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++;
    }
}

这会给你是这样的:

阅读全文

相关推荐

最新文章