2012-11-27 26 views
0

移動連續產生圖像我做了在隨機生成圖像的程序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); 
    } 

回答

0

你有不同的解決方案:

  • 僅有1定時器,其中更新的UIImageView的數組(我喜歡這個)
  • 子類的ImageView,把定時器類,所以每一個內將有自己的計時器
  • 也許你可以使用[UIView animateWithDuration...代替計時器,但不能確定是否可以同時使用多種。
+0

謝謝!這份名單有很大幫助。我想嘗試第二個對象..但我不確定如何繼承imageview。這是一個簡單的程序嗎? – user1824518

+0

只是創建一個類,使之從UIImageView的,而不是NSObject的繼承並添加您需要 – Luis

+0

對不起,所有的問題的代碼。現在我已經創建了新類(稱爲BirdImageView)並將計時器代碼放入其中,如何將我的UIImageView代碼引用到BirdImageView子類?感謝所有的幫助! – user1824518

0

的問題是:怎樣才能讓每0.5秒 所有這些代碼重複,使每一個新生成的圖像都有自己的moveObject計時器。

看看Core Animation的粒子效果 - 它們不僅僅適用於煙霧,火焰和火焰。通過設置粒子發射器並使用圖像創建粒子單元,可以讓Core Animation負責整個操作。我不明白的話題很多正式文件,但你可以閱讀的參考頁CAEmitterLayer得到它是如何工作的想法,然後看看the tutorial on raywenderlich.com

0

使用此function每個raindropobject。只是提供pointmove到:

-(void)raindropAnimation:(CGPoint)dropPoint 
{ 
    raindrop.frame = CGRectMake(dropPoint.x,0,raindrop.frame.size.width,raindrop.frame.size.height) //here y is 0 ie topmost 
    [UIView animateWithDuration:2.0 
         delay:0.0 
        options:UIViewAnimationCurveEaseInOut 
       animations:^ { 
         raindrop.frame = CGRectMake(dropPoint.x,dropPoint.y,raindrop.frame.size.width,raindrop.frame.size.height) //will move to point 
       } 
       completion:^(BOOL finished) { 
        [self perfromSelector:@selector(raindropAnimation:CGPointMake(xnewpointhere,ynewpointhere))]; 
       }]; 
+0

非常感謝這段代碼!不過,我有點不確定如何實現它。所以NSTimer會連接到這樣嗎?:moveObjectTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(raindropAnimation)userInfo:nil repeatats:YES]; – user1824518