最初にアニメーションを開始したい場合は、以下のようにアニメーションを開始します。
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];
}
上記のように変更を加えて、試してみてください。あなたは必要なものを手に入れるでしょう。