Why do anchor points vary in different resoliutions? - lua

I'm currently trying to place text in the four corners of the screen but the thing I came across was that in some screen resolutions(e.g. 1080*1920) the anchor points aren't right in the corner. the x values for some reason are the same, but the y changes, and is not close to the corner of the screen. Here is an example of me placing some text in the top right corner:
local myText = display.newText( "RIGHT", 0, 0, native.systemFont, 16 )
myText:setFillColor( 0, 0, 0 )
myText.anchorX = 1
myText.anchorY = 0
myText.x = display.contentWidth
myText.y = 0
I can't understand why this doesn't work for all screen resolutions.

Will this work for you:
-- Top
myText.y = display.screenOriginY;
-- Bottom
myText.y = display.contentHeight - display.screenOriginY;
-- Right
myText.x = display.contentWidth - display.screenOriginX;
-- Left
myText.x = display.screenOriginX;

Tha anchor points of display objects don't change.
The coordinate system of screen change depends on scaling mode. So top-left point not always will be (0, 0). For example in letterbox mode top-left point would be (display.screenOriginX, display.screenOriginY).
From Corona documentation
"letterbox" — scales the content area to fill the screen while preserving the same aspect ratio. The entire content area will reside on the screen, but this might result in "black bars" on devices with aspect ratios that differ from your content aspect ratio. Note, however, that you can still utilize this "blank" area and fill it with visual elements by positioning them or extending them outside the content area bounds. Essentially, "letterbox" is an ideal scale mode if you want to ensure that everything in your content area appears within the screen bounds on all devices.
"zoomEven" — scales the content area to fill the screen while preserving the same aspect ratio. Some content may "bleed" off the screen edges on devices with aspect ratios that differ from your content aspect ratio. Basically, "zoomEven" is a good option to ensure that the entire screen is filled by the content area on all devices (and content clipping near the outer edges is acceptable).
Letterbox
zoomEven
Read more about Content Scaling.

Related

How can I do to make AutoLayout fit all the size screen of iPhones

actually I'm trying to fit my app for all the size of screen phones, I have a view (the blue square) which contains 2 rectangles at top and bot and 4 squares in the 4 angles of the view and I'm struggling how to make my view (and the contains) have a dynamic resizing on different phone as you can see for the iPhone 4 it takes all the screen... Thanks for the help.
my AutoLayout image
Looks like you set a fixed width constraint (with a constant of 334) to your blue square view. The smaller iPhones (4s and SE) have a screen width of 320pt, so your blue square view is too wide for them.
You have to make the blue square view's width dynamic (depending on the available screen width)
To achieve that you can set the blue square view's width constraint the be the same as its superview (the light blue view) and then define a multiplier. For example to define the blue square view's width to be 80% of the available width, you set the multiplier to 0.8
Here are the steps to set the dynamic width:
Delete the fixed width constraint
Select the blue square view and the light blue view
Add a new Equal Widthsconstraint
Select the constraint
In the Attributes Inspector define the Multiplier

Resizing buttons and position on a static background image

I am working to have my buttons height and position adapt to screen size changes like the pictures shown above. The buttons themselves will remain clear and only serve as a simple way to handle taps that trigger the segues to different screens. My goal is to make it so that as the image stretches across different screen sizes, I would like the buttons to keep equal height and width and position with the windows. I know that if the windows had properties I could simply make the buttons have an equal size and width to them and be done, but as I mentioned the image is static and it has to stay that way for the time being. I've tried creating constraints for the buttons and that has only proven to be a headache and I don't know if stack views will help me here either, I know this is fairly complex, but I'm ok with that I just need some direction.
UPDATE: In an effort to follow the instructions LGP listed properly I started from step 1. As I mentioned in the comments, I believe it's simply the ratio and the constraints conflicts since when I remove one or two it works fine, but then how do I set the constraints so it fills the entire screen and maintains the ratio of the picture? Also shown are the constraint conflicts for the image view an it isn't showing the aspect ratio of the parent container view either
If you want to do it in interface builder it is not too hard. You should use spacer views and proportional sizes to position the buttons. That way, whatever size your background will have, all the elements will follow.
1. Create a container that has the same proportions as you image. Add a regular UIView and set an Aspect Ratio constraint with a multiplier of 852:568. This is the dimension of your background photo, 852 x 568 pixels, but the actual values don't matter, as long as the aspect ratio is the same. (Don't forget to also tie up the container view to however you want it in your view controller. See the UPDATE below on how to do this.)
2. Place the background image in the container. Add an image view as a child to the container. Set the constraints to touch all four edges of the container. Set the Image property to you image, and set Content Mode to Aspect Fit.
3. Add the first spacer view. Add a regular UIView to the container view (see leftmost, white view below) and set the constraints as follows:
height = 1 (not important, I used 10 in the image)
Top space to Superview = 90 (not important)
Leading space to Superview = 0
Width equal to Superview with multiplier dw:cw <- This makes it proportional! dw is the distance from the left edge to the first window/button, and cw is the width of the container. If your container is 375 wide, and your distance to the first button is 105, the multiplier will be 105:375.
4. Add the second space view. This is the vertical spacer, going from top to first button. Set it up similar as the first spacer, except make the height proportional to the containers height, and the width fixed.
5. Add the first button. Constrain its left and top edges to the spacers, then make its width and height proportional to the container.
6. Add the remaining spacers and buttons. They are all the same. Just remember where to make them proportional. All buttons are constraint to the single vertical spacer.
Finally, you should make the spacer views hidden. You can easily try it within your Storyboard by selecting different devices.
I chose to add everything on iPhone 8, but it is not really important. Here is what it looks like when I change to iPad Pro 12.9" and iPhone SE. Note how the buttons are positioned correctly. The spacer move around a little because they have partly fixed distances, but it works fine anyway.
UPDATE: Here is how to constrain the container view in the view controller's view to make the container fill the whole view and still keep its aspect ratio.
First, set the image view's (the one you added in step 2 above) Content Compression Resistance Priority to 200 for both Horizontal and Vertical. This will allow the image to compress rather then expand if it has a choice.
Second, Add the following constraints to you container:
Align Center X to Superview
Align Center Y to Superview
Equal Width to Superview >= 0
Equal Height to Superview >= 0
852:568 Ratio to View <- This you should already have!
Now the container will always center on screen, it will always fill at least the entire screen, but will also allow it to fill beyond in both X and Y.
UPDATE 2: If you want to ignore the photo's aspect ratio, and always fill the screen with the photo, you just add constraints for the container view to each side to its superview. Your container view's constraints should look like this.
In step 2 you will need to set the image's Content Mode to Scale to fill. The rest should be the same.
Use percentage based positions and size. Identify the positions of windows in percentage basis, and create the origin in x and y dimension by multiplying the percentage to the width and height of the screen. I am assuming that you are using ScaleToFill as content mode of the ImageView.
Similarly for calculating size, identify the width and height of the ImageView on percentage basis, and multiply the values in percent with the total width and height of the screen.
For example, to calculate the position of Window one-
Suppose, window1.x in percentage basis is 25% & total image view width is 400 (for example), than window1.x pixel position will be-
window1X = (25 * 400) / 100 = 100
Suppose, window1.y in percentage basis is 25% & total image view height is 300 (for example), than window1.y pixel position will be-
window1Y = (25 * 300) / 100 = 75
Suppose, width is 7% of image views width, than width in pixel will be -
window1Width = (7 * 400) /100 = 28
Suppose, height is 12% of image views height, than height in pixel will be -
window1Height = (12 * 300) /100 = 36
window1.frame = CGRectMake (window1X, window1Y, window1Width, window1Height)
Same approach for other windows, to calculate their positions(size will be same as window 1)
This approach will work across all screen resolutions, since it uses percentage based calculations & ScaleToFill as content mode for image view.

Corona SDK - rectangle not appearing in expected place (coordinate system)

I'm brand new to corona SDK and following tutorials. I notice that when I'm positioning elements theyre not appearing as they do in the tutorials. For example:
local testRect = display.newRect( 0, 0, 50, 50 )
testRect:setFillColor( 0.5,0,0 )
In the tutorial it looks like this:
I would guess the anchor point on the rectangle would be 0.5, 0.5 and I'd have to position it 50% to the right, and 50% from the top of the upper left corner. It looks like in their example the rectangle has an anchor point of 0, 0 and is stuck to the edge of the screen by default.
Why is there a difference? i notice this tutorial is using an iphone 4.. but should that matter?
A couple of years ago, Corona SDK went to their Graphics 2.0 standard. Part of this move was to get all display.* objects to behave the same when creating them. Before Graphics 2.0, some objects the X and Y values meant top left, for others it meant center. With Graphics 2.0, all X and Y values when creating an object are the center of the object now.
If you have an older tutorial, it may be assuming that the x, y is top left. In your example you are creating the object at 0, 0 which is the top left corner of the content area. Since you're on a screen that's wider than it's defined content area, 0, 0 isn't always the top left corner of the screen.
I'm guessing your config.lua is setting the width to 320 and the height to 480 (always listed as if the device was in portrait mode). But you're using an iPhone 5 screen, which means the top left corner is -44, 0. This explains why the image isn't all the way to the left edge.
I am sure the anchor points they using are 0.5,0.5.
I suggest you check your build.settings file .. I tried the code and got the same result they had ..
Again CHECK your build.settings file

How do I align circular buttons in a circle in both landscape and portrait orientations on iOS?

I would like to layout 5 buttons (that have a circular image) in a circular pattern (a kind of pentagon shape) in iOS. Ultimately it will also work on iPad, but at the moment, I'm just trying to get it working on iPhone.
So far, I have just tried putting them into a UIView and using constraints to lay them out via IB. However, when I rotate the device to landscape the layout gets messed up.
Here is a picture of the kind of layout I mean in IB which should ideally adapt to fit both iPhone portrait and landscape.
If I fix the height and width, it's clearly too big for iPhone:
I guess I need to find a way to have it maintain it's aspect ratio as a square and aligned to the middle of the parent view.
If I constrain to the top layout guide with horizontal space and vertical space it just looks like this:
I just can't seem to get my head around the right way to do this.
Any thoughts and pointers in the right direction greatly appreciated.
Many thanks.
You need to update your approach,
Logic : Use parametric equations to get points ( button centres ) along circumference of circle, Update constraints when screen rotates ( take outlets of constraints to modify ).
These formulae will give point ( x, y ) along circumference ( Centres of buttons / leading & top constraint of buttons )
x = cx + r * cos(a)
y = cy + r * sin(a)
Where,
r is the radius, ( in your case half of screen size )
cx,cy the origin, ( in your case centre of view )
and a the angle from 0..2PI radians or 0..360 degrees. ( Angle between two buttons 360/5 because there are 5 buttons )
I have used this formula in one of the control i have developed..
https://github.com/AdityaDeshmane/iOSCircularMenu
Check method - (void)setButtonFrames in following file
https://github.com/AdityaDeshmane/iOSCircularMenu/blob/master/iOSCircularMenu/CustomCircularMenuControlFiles/ADCircularMenuViewController.m

objective-c, psd, makeup

Does anybody know how to make correct image and (UIlabel)label position for iphone and ipad
according to the layout image in psd file.
I'm using photoshop. I take a ruler look for the highest letter in the sentence(which should be positioned) and move it to the top of psd image, than take the left extreme letter and move ruler to the left.
Then i make a screenshot of the ipad/iphone screen and compare the x,y of psd and png file.
if psd x/y more then png x/y i make the next steps: Xpsd - Xpng, Ypsd - Ypsd, and if the coordinates of psd less than png i make Xpsd + Xpng, Ypsd + Ypsd.
And finally i have wrong visual position in ipad/iphone!
The most common reasons why the visual position/dimensions of a given element don't match what you think they should is typically the auto resizing mask that is applied to the particular view in question.
For example, your UILabel should have its autoresizing mask set to:
label.autoResizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
This will keep your label aligned to the top left, and will not allow its height, nor its width to be changed depending on the super view's size changes.
This can also be set in interface builder with these controls:
The outer lines match the margin sizing, if it is a solid red line, then that means that as the superview's size changes, the current view's position relative to those edges will be preserved.
The Inner lines represent a flexible width, and height. If it is a solid line, then that means that the current view will resize to retain the same proportion of the superview's size.

Resources