1
我做了一個程序,在屏幕頂部的隨機x座標處生成圖像。圖像然後落到底部。但是,我希望每隔幾秒鐘不斷生成新的圖像(同一圖像),以便彷彿同一圖像的這些副本從頂部持續「下雨」。循環多種方法
我該如何讓這些代碼每0.5秒重複一次?
@implementation ViewController {
UIImageView *_myImage;
}
- (void)viewDidLoad
{
srand(time(NULL));e
int random_x_coordinate = rand() % 286;
CGRect myImageRect = CGRectMake(random_x_coordinate, 0.0f, 40.0f, 40.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"flake.png"]];
myImage.opaque = YES;
[self.view addSubview:myImage];
_myImage = myImage;
//FALLING BIRDS TIMER
moveObjectTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(moveObject) userInfo:nil repeats:YES];
}
//FALLING BIRDS MOVER
-(void) moveObject { // + means down and the number next to it is how many pixels y moves down per each tick of the TIMER above
_myImage.center = CGPointMake(_myImage.center.x, _myImage.center.y +1);
}
檢查您是否可以使用動畫來創建秋季效果。您可能需要在for循環中創建UIImageView,以便爲多個圖像執行此操作,然後將其存儲在數組中或進行動畫處理。 – iDev