2012-04-10 73 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