移動連續產生圖像我做了在隨機生成圖像的程序x座標。圖像然後落到底部。但是,我希望每隔幾秒鐘不斷生成新的圖像(同一圖像),以便彷彿同一圖像的這些副本從頂部持續「下雨」。 (注意:最終,當我繼續開發這個應用程序時,我需要隨時回想每個圖像的位置,所以我相信我需要每個衍生的圖像成爲數組的一部分。我也相信我必須將每個圖像圖像一步一步,所以我不能依靠動畫)。xcode中:在屏幕的頂部,在一個方向
的問題是:怎樣才能讓每0.5秒所有代碼重複,使每一個新生成的圖像都有自己的moveObject計時器。它會像雨滴從頂部落下。
@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);
}
謝謝!這份名單有很大幫助。我想嘗試第二個對象..但我不確定如何繼承imageview。這是一個簡單的程序嗎? – user1824518
只是創建一個類,使之從UIImageView的,而不是NSObject的繼承並添加您需要 – Luis
對不起,所有的問題的代碼。現在我已經創建了新類(稱爲BirdImageView)並將計時器代碼放入其中,如何將我的UIImageView代碼引用到BirdImageView子類?感謝所有的幫助! – user1824518