How to add "Align Center Y to" constraint? - xcode

In InterfaceBuilder, is there a way to add an
Align Center Y to
constraint? I can't find this type of constraint anywhere and I'm not sure how to create it by control + drag.

Align center Y to` = Center Vertically
Align center X to = Center Horizontally.
Press control on your element, hold it and link it to an other element, select Center ....

Related

Scrollview doesn't scroll in Xcode

I have a scroll view with the constraints:
equal width to Safe Area
equal height to Safe Area
Align Center X to Superview
Align Center Y to Superview
It contains only a vertical Stack View with the following constraints:
Align Center X to: SuperView
Proportional Width to Superview (multiplier 0.9)
Top Space to Superview: 0
Now, if I add enough items to the stack view, so that it exceeds the bottom of the Scrollview, I can't scroll it. Why is that so and how can I make it work?
here is my storyboard:
https://drive.google.com/file/d/1TZHl3d_HCptqx8ezcOpNJjH8RSitOln5/view?usp=sharing

Alignment of buttons in view

I have 4 buttons in an row, how can i align this in the middle on each device? iphone 4s, iphone 5s, iphone 6, iphone 6 plus.
Only on the iphone 6 it looks good. On the iphone 4s the buttons going outside the view and on the 5s and 6 plus is it not centered.
On the left view you see the layout, and the right view you see an preview.
How can i fix this issue? I have tried auto layout but this doesn't work..
Here with the constraint alignment (horizontal) on each button. But they are not in the middle..
thank you.
To align multiple buttons in the center of the canvas, you can place two "dummy" or "helper" views on each side. Then apply the following constraints:
equal width for the two dummy views.
leading space to superview = 0 for the left dummy view.
trailing space to superview = 0 for the right dummy view.
horizontal spacing = 0 for the dummy view and the button.
horizontal spacing = some fixed value for the buttons.
You can set the dummy view's height to 0.
Below is an example in the storyboard. For the sake of simplicity, I only included two buttons. You can have as many buttons as you like using this method.
I might be missing something here. But is it all that you need, to have multiple buttons centered vertically in the container and also horizontally (with no overlap and good symmetry). If so, you can do the below.
The constraints are -
Button A - Center vertically in container
Button B:Center Y = Button A:Center Y; Button C:Center Y = Button B:Center Y
Button A: Leading space to superview = some constant (say 30)
Button C: Trailing space to superview = same constant as above
Horizontal spacing (Button A - Button B) = Horizontal spacing (Button B - Button C) = some constant (say 10)
Width (Button A) = Width (Button B) = Width (Button C)

What is the Google Map zoom algorithm?

I'm working on a map zoom algorithm which change the area (part of the map visible) coordinates on click.
For example, at the beginning, the area has this coordinates :
(0, 0) for the corner upper left
(100, 100) for the corner lower right
(100, 100) for the center of the area
And when the user clicks somewhere in the area, at a (x, y) coordinate, I say that the new coordinates for the area are :
(x-(100-0)/3, y-(100-0)/3) for the corner upper left
(x+(100-0)/3, y+(100-0)/3) for the corner upper right
(x, y) for the center of the area
The problem is that algorithm is not really powerful because when the user clicks somewhere, the point which is under the mouse moves to the middle of the area.
So I would like to have an idea of the algorithm used in Google Maps to change the area coordinates because this algorithm is pretty good : when the user clicks somewhere, the point which is under the mouse stays under the mouse, but the rest of area around is zoomed.
Somebody has an idea of how Google does ?
Lets say you have rectangle windowArea which holds drawing area coordinates(i.e web browser window area in pixels), for example if you are drawing map on the whole screen and the top left corner has coordinates (0, 0) then that rectangle will have values:
windowArea.top = 0;
windowArea.left = 0;
windowArea.right = maxWindowWidth;
windowArea.bottom = maxWindowHeight;
You also need to know visible map fragment, that will be longitude and latitude ranges, for example:
mapArea.top = 8.00; //lat
mapArea.left = 51.00; //lng
mapArea.right = 12.00; //lat
mapArea.bottom = 54.00; //lng
When zooming recalculate mapArea:
mapArea.left = mapClickPoint.x - (windowClickPoint.x- windowArea.left) * (newMapWidth / windowArea.width());
mapArea.top = mapClickPoint.y - (windowArea.bottom - windowClickPoint.y) * (newMapHeight / windowArea.height());
mapArea.right = mapArea.left + newWidth;
mapArea.bottom = mapArea.top + newHeight;
mapClickPoint holds map coordinates under mouse pointer(longitude, latitude).
windowClickPoint holds window coordinates under mouse pointer(pixels).
newMapHeight and newMapWidth hold new ranges of visible map fragment after zoom:
newMapWidth = zoomFactor * mapArea.width;//lets say that zoomFactor = <1.0, maxZoomFactor>
newMapHeight = zoomFactor * mapArea.height;
When you have new mapArea values you need to stretch it to cover whole windowArea, that means mapArea.top/left should be drawn at windowArea.top/left and mapArea.right/bottom should be drawn at windowArea.right/bottom.
I am not sure if google maps use the same algorithms, it gives similar results and it is pretty versatile but you need to know window coordinates and some kind of coordinates for visible part of object that will be zoomed.
Let us state the problem in 1 dimension, with the input (left, right, clickx, ratio)
So basically, you want to have the ratio to the click from the left and to the right to be the same:
Left'-clickx right'-clickx
------------- = --------------
left-clickx right-clickx
and furthermore, the window is reduced, so:
right'-left'
------------ = ratio
right-left
Therefore, the solution is:
left' = ratio*(left -clickx)+clickx
right' = ratio*(right-clickx)+clickx
And you can do the same for the other dimensions.

Matlab: Flipping image coordinates to cartesian coordinates

I am new to Matlab. I have an image (the size is mxnx3) with a few human-selected points on the image. For example:
p1 = [267,79];
p2 = [96,372];
These points are image coordinates with (1,1) at the top left. I'm trying to convert this to Cartesian coordinates with (0,0) on the bottom left. How can I do this? Thanks in advance!
If I understand correctly: just use
axis xy
From axis doc:
AXIS XY puts MATLAB into its default "Cartesian" axes mode. The
coordinate system origin is at the lower left corner. The x
axis is horizontal and is numbered from left to right. The y
axis is vertical and is numbered from bottom to top.
If you need to directly translate your co-ordinates in code, you could make a simple anonymous function:
img2cart = #(p) [p(1), img.size(2) - p(2)];
q1 = img2cart(p1);
q2 = img2cart(p2);

How to get the Position & Dimension of a Shape in Powerpoint?

I'm playing around with OpenXmlSDK to see if it's a viable solution for our Powerpoint needs. One thing that is required is the ability to position shapes in the Powerpoint. I've been searching around for a way to get the position of a Shape, but have only come across is the MSDN "How To" http://msdn.microsoft.com/en-us/library/cc850828.aspx and a Position class (but no way to get it from a Shape) http://msdn.microsoft.com/en-us/library/office/documentformat.openxml.wordprocessing.position%28v=office.14%29.aspx.
How do I do something like:
PresentationDocument presentationDocument = PresentationDocument.Open("C:\\MyDoc.pptx", true);
IdPartPair pp = presentationDocument.PresentationPart.SlideParts.First().Parts.FirstOrDefault();
var shape = pp.OpenXmlPart;
// How do I get the position and dimensions?
You have 2 variables for the dimension of the shape :
- Offset gives the position of the top corner of your shape
- Extents gives the size off your shape
shape.ShapeProperties.Transform2D.Offset.X //gives the x position of top left corner
shape.ShapeProperties.Transform2D.Offset.Y //gives the y position of top left corner
shape.ShapeProperties.Transform2D.Extents.X //gives the x size of the shape : the width
shape.ShapeProperties.Transform2D.Extents.Y //gives the y size of the shape : the height
Go through the XML for the slide in question and look for xfrm elements, which should contain off (offset) and ext (extent) sub-elements. The measurements are in EMUs (see last page of Wouter van Vugt's document).
Sometimes ShapeProperties is not displayed as a Shape property, you must write
var sP = ((DocumentFormat.OpenXml.Presentation.Shape)shape).ShapeProperties;
After you can use Transform2D and find coordinates as Deunz wrote.

Resources