2012-04-10 68 views
0

我想要使UIImageview動畫在屏幕上隨機移動。對於一個UIImageView,它工作正常。但是對於兩個或更多的對象它不起作用。誰能幫我?在屏幕上隨機移動兩個或多個對象

-(void)animationLoop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {   
    [UIView beginAnimations:nil context:nil];   
    [UIView setAnimationDuration:1];    
    CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width);   
    CGFloat y = (CGFloat) (arc4random() % (int) self.view.bounds.size.height);   
    CGPoint squarePostion = CGPointMake(x, y);   
    image.center = squarePostion;  
    [UIView setAnimationDelegate:self];  
    [UIView setAnimationDidStopSelector:@selector(animationLoop:finished:context:)]; 
    [UIView commitAnimations];  
} 

在此先感謝

+1

顯示迄今爲止所做的工作。 – 2012-04-10 08:51:51

+0

是的,請添加相關的代碼片段 – giorashc 2012-04-10 08:56:08

+0

我已經添加了我寫的代碼。 – Priya 2012-04-10 09:00:30

回答

1

最初,當你要啓動動畫,開始動畫如下。

NSNumber *number1 = [NSNumber numberWithInt:0]; 
[UIView beginAnimations:nil context:number1];   
[UIView setAnimationDuration:2.0];    
CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width);   
CGFloat y = (CGFloat) (arc4random() % (int) self.view.bounds.size.height);   
CGPoint squarePostion = CGPointMake(x, y);   
imgView1.center = squarePostion;  
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationLoop:finished:context:)]; 
[UIView commitAnimations];  


number1 = [NSNumber numberWithInt:1]; 
[UIView beginAnimations:nil context:number1];   
[UIView setAnimationDuration:2.0];    
x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width);   
y = (CGFloat) (arc4random() % (int) self.view.bounds.size.height);   
squarePostion = CGPointMake(x, y);   
imgView2.center = squarePostion;  
[UIView setAnimationDelegate:self];  
[UIView setAnimationDidStopSelector:@selector(animationLoop:finished:context:)]; 
[UIView commitAnimations];  

而你的animationLoop:finished:context:方法應該如下。

-(void)animationLoop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {   
    [UIView beginAnimations:nil context:context];   
    [UIView setAnimationDuration:2.0];    
    CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width);   
    CGFloat y = (CGFloat) (arc4random() % (int) self.view.bounds.size.height);   
    CGPoint squarePostion = CGPointMake(x, y); 

    int i = [(NSNumber*)context intValue]; 
    if(i == 0) 
    { 
     imgView1.center = squarePostion;  
    } 
    else 
    { 
     imgView2.center = squarePostion;  
    } 
    [UIView setAnimationDelegate:self];  
    [UIView setAnimationDidStopSelector:@selector(animationLoop:finished:context:)]; 
    [UIView commitAnimations];  
} 

如上所述進行更改並嘗試。你會得到你需要的。

+0

這是通過2個圖像瀏覽完成的。如果你有一系列imageviews,那麼你可以用'for'循環來做到這一點。 – Ilanchezhian 2012-04-10 10:37:37

+0

如何寫這行CGFloat x =(CGFloat)(arc4random()%(int)self.view.bounds.size.width);迅速。 – Amanpreet 2017-02-02 08:14:43

+0

@Ilanchezhian根據你的回答,我們是否需要將對象聲明爲類變量來做到這一點?假設對象是動態添加的,我們是否需要將它們聲明爲類變量?在這個例子中,imgView1和imgView2被聲明爲類變量。如何在屏幕上顯示超過10個對象? – 2017-06-21 11:24:14