简体   繁体   English

在屏幕上随机移动两个或多个对象

[英]Move two or more objects randomly around the screen

I am trying to animate UIImageview to move randomly around the screen.我正在尝试为 UIImageview 设置动画以在屏幕上随机移动。 For one UIImageView it is working fine.对于一个 UIImageView,它工作正常。 But for two or more objects it is not working.但对于两个或多个对象,它不起作用。 Can anyone help me?谁能帮我?

-(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];    
} 

Thanks in advance提前致谢

Initially when you want to start the animation, start the animation as below.最初当你想启动 animation 时,如下启动 animation。

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];    

And your animationLoop:finished:context: method should be as follows.你的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];    
} 

Make the changes as mentioned above and try it.如上所述进行更改并尝试。 You would get what you required.你会得到你需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM