Search code examples
iphonexcodeanimationcoordinateslandscape

xcode animation ball iphone problem landscape mode


Hi everyone I'm french so scuse me for my english. My problem is that I'm animating a ball on the screen in portrait mode, but now I want do to the same thing in landscape mode. Everything work but when the ball hit a side of the iphone it go throught it. this code doesn't work I think that the problem is about x and y.

if (ball1.center.x > 480 ||ball1.center.x < 0){ ajout.x = -ajout.x; } if (ball1.center.y > 320 ||ball1.center.y < 0){ ajout.y = -ajout.y;


Solution

  • Yes your problem is with the x and y. In portrait mode that is correct with x being 480 and y being 320. However in landscape the two values are reversed.

    Try this code instead;

    CGRect _frame = [[UIScreen mainScreen] bounds];   
    
    if (ball1.center.x > _frame.size.width || ball1.center.x < 0)
        ajout.x = -ajout.x;
    
    if (ball1.center.y > _frame.size.height || ball1.center.y < 0)
        ajout.y = -ajout.y;
    

    This should return the propery bounds of your screen (320x480 in portrait, 480x320 in landscape) and check against those values.

    Cheers.