0

How can I programmatically spawn an unlimited number of UIImageViews on to the screen in a random location on a time increment. The random location would be:CGPoint(arc4random() % (481), arc4random() % (321),20,20) I just don't understand how to constantly create new ones with one image variable and a for loop. Or an NSTimer that on every increment adds a new image to the screen.

I know there are some tutorials out there for this, but the only ones I could find used automatic reference counting. It should be simple to create, and a link is fine.


Other questions:

UI is for user-interface, but since i'm not using a storyboard or xib, would there be a better type of imageView to use.

4

1 に答える 1

1

ビューコントローラクラスで(もちろん独自の画像ファイル名に置き換えて)、このようなものはどうですか?

- (void)viewDidLoad {
    [super viewDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(addViews:) userInfo:nil repeats:YES];
}

-(void)addViews: (NSTimer *) aTimer {
    UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"New_PICT0011.jpg"]];
    CGRect rect = CGRectMake(arc4random() % (481), arc4random() % (321), 20, 20);
    [iv setFrame:rect];
    [self.view addSubview:iv];
}

そこになんらかのカウンターを配置する必要があり、必要なビューの総数に達したら、[aTimerinvalidate]を呼び出します。

于 2012-09-15T02:25:00.050 に答える