2

我正在尝试创建几个 UIImageViews(最好使用 for 循环),然后移动它们,但我不知道如何移动它们,因为如果以编程方式制作它们,我无法弄清楚如何引用它们。

所以我想要这样的东西:

    for (int i = 0; i < 2; i++)
    {
        UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed: picturo]];
        CGRect rect = CGRectMake((i * 50) + 50,30, 25, 25);
        [iv setFrame:rect];
        iv.tag = i;
        [self.view addSubview:iv];
    }

    (iv with tag of 0).center.y = 80; // <-- How do I do that!!

显然这个例子没有实际用途,我可以将 'rect' 以上的 y 值更改为 80,但我想知道如何:

一个。创建多个 uiimageviews(或任何对象)并能够单独识别/引用/操作每个(我不知道如何,但我会假设通过唯一命名它们或使用标签)和

湾。能够识别/引用/操作以编程方式创建的对象。

4

1 回答 1

1

creating views with tags works the way you have it.

To access those views use the viewWithTag property on a view to identify one of it's subviews:

  UIView* subview = [self.view viewWithTag:0];
  subview.center = CGPointMake(subview.center.x, 80);

(you cannot write to a view.center's x or y, you have to recreate the whole thing. Likewise with other geometric structs such as CGSize, CGRect)

于 2013-01-28T02:29:28.143 回答