xcode animation ball iphone problem landscape mode - iphone

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;

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.

Related

OpenGL Scene - Iphone

Question: why would an OpenGL scene render upside down in portrait mode but right-side up in landscape and visa-versa?
If I create a view matrix with UPx = 0, UPy = 0, UPz = 1 I get my viewport upside down in portrait mode and right-side up in landscape.
If I create a view matrix with UPx = 0, UPy = 0, UPz = -1 I get my viewport right-side up in portrait mode and upside down in landscape.
Should I be looking at other functions other than where I create my LookAt and Perspective matrices?
Got it - my FOVY angle was in degrees for Android - but GLKit for IOS requires radians. Dohhh.

Screen size in pixels (iPhone 6)

According to the page here: http://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions the iPhone 6 screen size in pixels should be 750×1334 (1334x750 landscape) however my application seems to think the screen dimensions are 667/375 in landscape and it's not being rendered properly to the entire screen. Here's the code I'm using to get the window dimensions when the application launches as well as opengl.
applicationDidFinishLaunching:
CGRect rect = [[UIScreen mainScreen] bounds];
CGFloat screenW = rect.size.width;
CGFloat screenH = rect.size.height;
initGL:
CGRect rect = [glView bounds];
CGFloat screenW = rect.size.width;
CGFloat screenH = rect.size.height;
glOrthof(0, screenW, 0, screenH, -1, 1);
Both functions print out the same values: 667w/375h. Could this have to do with an issue I had with the splash image, where basically I had to rename it from Default-667h#2x.png to Default-375w-667h#2x.png to get it to load properly? I feel like this is something simple I'm missing here, any help is appreciated.
It is is ok. What you see is logical points. These get translated into physical pixels. In case of iPhone 6 it is 2x.

Convert coordinates from iPhone screen size to iPad screen size

I have a custom UIView that contains an interactive drawing that is drawn in the drawRect function of the view. On the iPad the drawing size is 1024 x 768. For the iPhone I shrink the drawing for the iPhone using CGContextScaleCTM. To keep the proper aspect ratio on my drawings I shrink the view to 480 x 360 and set the y value of the view to -20 (effectively cropping 20 pixels off the top and bottom of the View, which is fine). Everything looks correct now, but I need to convert the touch coordinates from iPhone to iPad coordinates for the interactive portions of my view to work. If I make the uiview 320 high and use
point.y *=768/320
for converting the y value the locations are correct (but my drawing is distorted) I've done some tests hard coding point in so I know this should work but I'm having a hard time getting the math to work with the crop. Here is what I have so far:
CGPoint point = [[touches anyObject] locationInView:self.view];
[self endTouch:&point];
NSLog(#"true point: %#",NSStringFromCGPoint(point));
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
point.x *= 1024/480;
point.y *= 768/360;
NSLog(#"corrected point: %#",NSStringFromCGPoint(point));
}
Can anybody help me with doing this conversion? Thanks!
1024/480 and 768/360 perform integer division. The result of both operations is 2, but you really want 2.133333. Replace the relevant lines in your code with the following:
point.x *= 1024.0/480.0;
point.y *= 768.0/360.0;
This will return the value you're looking for and scale your point's x and y values accordingly.
You might be better served (in terms of readability) by replacing these literals with a #define macro. At the top of your code, put the following:
#define SCALING_FACTOR_X (1024.0/480.0)
#define SCALING_FACTOR_Y (768.0/360.0)
and then modify your code to use that:
point.x *= SCALING_FACTOR_X;
point.y *= SCALING_FACTOR_Y;
That way, it will be more clear as to what you're actually doing.

iPhone correct landscape window coordinates

I am trying to get the window coordinates of a table view using the following code:
[self.tableView.superview convertRect:self.tableView.frame toView:nil]
It reports the correct coordinates while in portrait mode, but when I rotate to landscape it no longer reports correct coordinates. First off, it flips the x, y coordinates and the width and height. That's not really the problem though. The real problem is that the coordinates are incorrect. In portrait the window coordinates for the table view's frame are {{0, 114}, {320, 322}}, while in landscape the window coordinates are {{32, 0}, {204, 480}}. Obviously the x-value here is incorrect, right? Shouldn't it be 84? I'm looking for a fix to this problem, and if anybody knows how to get the correct window coordinates of a view in landscape mode, I would greatly appreciate it if you would share that knowledge with me.
Here are some screenshots so you can see the view layout.
Portrait: http://i.stack.imgur.com/IaKJc.png
Landscape: http://i.stack.imgur.com/JHUV6.png
I've found what I believe to be the beginnings of the solution. It seems the coordinates you and I are seeing are being based on the bottom left or top right, depending on whether the orientation is UIInterfaceOrientationLandscapeRight or UIInterfaceOrientationLandscapeLeft.
I don't know why yet, but hopefully that helps. :)
[UPDATE]
So I guess the origin of the window is 0,0 in normal portrait mode, and rotates with the ipad/iphone.
So here's how I solved this.
First I grab my orientation, window bounds and the rect of my view within the window (with the wonky coordinates)
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect windowRect = appDelegate.window.bounds;
CGRect viewRectAbsolute = [self.guestEntryTableView convertRect:self.guestEntryTableView.bounds toView:nil];
Then if the orientation is landscape, I reverse the x and y coordinates and the width and height
if (UIInterfaceOrientationLandscapeLeft == orientation ||UIInterfaceOrientationLandscapeRight == orientation ) {
windowRect = XYWidthHeightRectSwap(windowRect);
viewRectAbsolute = XYWidthHeightRectSwap(viewRectAbsolute);
}
Then I call my function for fixing the origin to be based on the top left no matter the rotation of the ipad/iphone.
It fixes the origin depending on where 0,0 currently lives (depending on the orientation)
viewRectAbsolute = FixOriginRotation(viewRectAbsolute, orientation, windowRect.size.width, windowRect.size.height);
Here are the two functions I use
CGRect XYWidthHeightRectSwap(CGRect rect) {
CGRect newRect;
newRect.origin.x = rect.origin.y;
newRect.origin.y = rect.origin.x;
newRect.size.width = rect.size.height;
newRect.size.height = rect.size.width;
return newRect;
}
CGRect FixOriginRotation(CGRect rect, UIInterfaceOrientation orientation, int parentWidth, int parentHeight) {
CGRect newRect;
switch(orientation)
{
case UIInterfaceOrientationLandscapeLeft:
newRect = CGRectMake(parentWidth - (rect.size.width + rect.origin.x), rect.origin.y, rect.size.width, rect.size.height);
break;
case UIInterfaceOrientationLandscapeRight:
newRect = CGRectMake(rect.origin.x, parentHeight - (rect.size.height + rect.origin.y), rect.size.width, rect.size.height);
break;
case UIInterfaceOrientationPortrait:
newRect = rect;
break;
case UIInterfaceOrientationPortraitUpsideDown:
newRect = CGRectMake(parentWidth - (rect.size.width + rect.origin.x), parentHeight - (rect.size.height + rect.origin.y), rect.size.width, rect.size.height);
break;
}
return newRect;
}
This is a hack, but it works for me:
UIView *toView = [UIApplication sharedApplication].keyWindow.rootViewController.view;
[self.tableView convertRect:self.tableView.bounds toView:toView];
I am not sure this is the best solution. It may not work reliably if your root view controller doesn't support the same orientations as the current view controller.
You should be able to get the current table view coordinates from self.tableView.bounds
Your code should be:
[tableView convertRect:tableView.bounds toView:[UIApplication sharedApplication].keyWindow];
That will give you the view's rectangle in the window's coordinate system. Be sure to use "bounds" and not "frame". frame is the rectangle of the view in its parent view coordinate system already. "bounds" is the view rectangle in its own system. So the above code asks the table view to convert its own rectangle from its own system to the window's system. Your previous code was asking the table's parent view to convert the table's rectangle from the parent coordinate system to nothing.
Try bounds instead of frame
self.parentViewController.view.bounds
for it gives me adjusted coords according to the current orientation

UIWebView - Rotating Causes Non-Interactable Areas

I'm having a strange problem with UIWebView. I have to set it's orientation manually by rotating and sizing it. But after i've done that there are areas of the web content that aren't usable.
The iPad is in Portrait, but the web content is built in Landscape. So what I do is rotate the container by 90 degrees, set the bounds of the container, and resize the container and webframe.
Here's the code I use for this:
//rotate
CGRect contentRect;
CGAffineTransform transform;
transform = CGAffineTransformMakeRotation(-3.14159/2);
contentRect = CGRectMake(0,0,1024,768);
//set container bounds and transform
[_container setBounds:contentRect];
[_container setTransform:transform];
//set frame
containerFrame = CGRectMake(0,0,768,1024);
webViewFrame = CGRectMake(0,0,768,1024);
[_webview setFrame:webViewFrame];
[_container setFrame:containerFrame];
So after this rotation happens, the ipad is still in Portrait, the web content is displayed correctly on the screen (rotated). But content below 768 pixels is not interactable.
I'm not sure what's going on. This has stumped me for a while now. Any ideas?