Getting Screen Size XCode - ios

I'm using spritekit to make an iOS game, and I came across a problem I've never had before. I want my game to be available on multiple devices, so I'm dynamically sizing everything based on the screen size, which I'm obtaining by using UIScreen.mainScreen().bounds.size, as was recommended online.
It's returning a screen size of 667x375. However, when I place an object at half the height and half the width, expecting to see it in the middle of the screen, it's instead displayed in the lower left corner.
Curious, I had it print touch locations from mouse clicks and discovered that the top right corner returns 1024x764 when clicked.
I've never had this problem before, and this is my 3rd XCode project. Can anyone help me?

UIScreen.mainScreen().bounds.size returns the size of your screen.
Sprite Kit uses a coordinate system different from a UIView. The bottom left corner of the scene is (0, 0). The values increase as you move up and to the right, so the top-right corner will give you the maximal value.
A scene's contents are scaled to fit the view based on the scaleMode property.
I suggest you re-read the Building Your Scene documentation.

Related

How can I maintain the proportional position of a view after a rotation?

What's the secret behind apps like Apple's Camera app, which can maintain the position of a view on the physical screen between rotations? I mean like when you rotate the Camera app, and the icons keep their position albeit spinning accordingly. (Messenger does this too when taking a picture for instance)
I've tried Auto Layout, setting the frame, viewWillTrasitionToSize(_:withTransitionCoordinator:), viewWillLayoutSubviews(), everything; but nothing seems to get the job done.
Even when invalidating the constraints and making new ones, or when resetting the frame, it still leaves its position for a moment before adopting the new coordinates.
So, does anyone how they do it? Or at least a way to replicate it?
Thanks

Guidance - Custom UICollectionView animation

I am looking for guidance with how to approach the following animation scenario.
I have UICollectionView with a custom circular UICollectionViewLayout subclassed layout. There are (circular) images around the perimeter of the circle and a 50% larger (circular) image in the center of the circle.
When the user taps one of the perimeter images, I want the tapped on image to move toward the center (becoming 50% bigger) and sort of "connect with" the center image. I then want the two images to float to the top of the view. (The bottom portion of the view will then be a context of things related to the two joined images).
If this makes sense to anyone, I'd really appreciate a starting point of some kind.
By the way, I'm an old developer but new to iOS and developing in Swift (but can read Objective-C).
Thanks!
I solved this using a visual trick. Of course, I'm sure there's a better way.
When one of the perimeter circles is tapped, I transition to a view controller that immediately creates a duplicate of the center circle and the tapped circle in their former locations, making it appear that the other elements disappeared.
I then simultaneously animate the tapped circle to the center and the center image to the top of the view. I enlarge the tapped circle briefly and then animate it to the top of the view next to the center circle restoring the tapped image's original size.
Not elegant but it works.

Problems rotating in the middle of a pan with UIViewPageController-based slide show

I have an image slide-show viewer that uses a UIPageViewController to present the images.
The image-view viewer ViewController is pretty simple -- a top-level view containing a UIScrollView containing a UIImageView. On initial image presentation or when the device is rotated the image is aspect-fitted to the view dimensions and centered. It works fine except for a problem which happens on both iOS 7 and iOS 6.
If I am in the middle of panning to change images and two images are on the screen and I then rotate the device it sometimes (maybe always?) messes up the display of one or more images. The previously-centered image appears in the wrong place on the screen and this persists when rotated and zoomed.
The only thing I can find wrong when this happens is that the center property of the UIImageView is not, in fact, the center of the frame. If I change the center property of the UIImageView in viewDidAppear to be the center of the possibly-scaled UIImageView then the images seem to display correctly in all cases.
Does this sound like the effect of some familiar mistake that I'm making?
Edit to respond to the question:
I don't do anything in the willRotate or didRotate methods.
In viewDidLayoutSubviews I aspect-fit the image into the current UIScrollView bounds (which is the full top-level view which is the full screen) and center it. This takes care of both the initial presentation and rotations. I don't see any problems in "normal" rotation situations. The problem occurs only when I rotate with two of the pageViewController's children views both partly onscreen at once. I can "fix" the problem in all cases but surely I'm doing something wrong.
Edit 2
I've discovered another anomaly that occurs less frequently when rotating the device while panning with 2 images onscreen. Best explained by an example:
The page view controller is being used to display one image in a sequence of images from left to right of A B C. Image B is onscreen. During a pan to the left you see part of both images A and B. If the pan to image A is nearly complete, i.e. B is nearly offscreen, and you rotate the device then image B normally ends up onscreen (it never rotates still showing the partial pan), but sometimes image C ends up being displayed -- as though you had panned in the other direction. Anybody else see this kind of behavior?
What are you doing in your UIViewController to prepare for the rotation?
– willRotateToInterfaceOrientation:duration:
And then how do you recover once the rotation has finished?
- didRotateFromInterfaceOrientation
These calls are provided specifically so you have a chance to get ready for a rotation, and then to put everything back to normal once it completes.

iPad - redrawing the arrow direction of a visible UIPopOverController

I have an object that is drawn near the first third of the screen. Imagine that there are 300 pixels between the top of the object (an image, for example) and the top of the screen.
When I show a popover, lets say, with 200x200 pixels and specify arrow direction as any, the popover may appear over the object, that is, between the object and the top of the screen.
If the popover for some reason, loads another view and is resized to more than the available space between the object and the top of the screen, it will not be able to fit there. iOS solves that, forbidden the popover to resize to more than possible. The result is that the content gets truncated and probably the popover will move a little bit and the arrow will point to nowhere.
Is there any way to make a popover that has increased to recalculate the best arrow direction?
thanks.
You can calculate the size of your needed future content (contentView) and compare it to your threshold and force the arrow direction. you may need to experiment with your threshold as the size of a content view isnt the entire height of the popover.
int popoverArrowDirection 15;//any
int threshold = 300;
if(viewThatWillbeInserted.frame.size.height>threshold)popoverArrowDirection = 1;//up
[myPopoverController presentPopoverFromBarButtonItem:myBarButton permittedArrowDirections:popoverArrowDirection animated:YES];
If you cant know this height before then you don't have many options.
The limitations of popovers drove me to mock-up a popover as Custom ViewController (like the page popover you see when you slide through the pages in a ibook; thats not an actual popover component). This would mean that if you had a down arrow, and pushed on a new bigger view you could even animate the view spinning around the origin of arrow tip or any other stupid animations..
I have only done this in enterprise apps, and am unsure weather this is app-store safe!
What happens if you dismiss the popover and then re-present it after the content size has changed?

UIViews display at wrong location in landscape mode

I am working on an diagramming application which displays boxes as subviews inside a larger view. The user can move the boxes around. I am persisting the positions of these boxes using core data into a sqlite database by holding the height, width, x and y (of the top-left corner). When I open up a diagram I am reading these values from the database and using them to set the frame of the subview within the main view.
In portrait mode this works fine and the boxes are repositioned at the correct points on the display. However, when I rotate into landscape, or start the application in landscape and open the diagram, the positions of the boxes are wrong.
I have traced through the code and can see that the correct values are coming back from the persistent store and being used to correctly set the frame. However, once the UIView for the subview is on screen it is positioned at a different location (clearly visible and confirmed by checking the values in the frame of the view).
Now the odd thing is that instead of being a fixed pixel offset, the x co-ordinate is always 8.5% larger than the value used to create the frame, and the y value is always 9.1% smaller.
Does anyone have any explanation as to why this might be and how I might solve the problem?
If it provides any clues, the main view is inside a UIScrollView.
Okay, I found the answer. I needed to set Autoresize Subviews to false on the UIScrollView. Having done that (by un-checking the appropriate box in IB) everything seems to be working fine.

Resources