Confusing (x, y) origins with sub-views in interface builder - xcode

I have a view in interface builder, and then a subview within that view. I place the sub-view visually near the upper left corner within its parent. I would therefore expect the x, y coordinates of the sub-views frame to be something close to (20,20), but instead IB tells me it is something like (230,432). If I then adjust the X, Y coords in the properties window to something like (20,20), the sub-view flies off the screen towards the upper-left... What is the deal? BTW, it is an iPad specific view size, where the top/parent view is sized 768x1004.

In the Size & Position section, in the grid on the left, make sure the top-left corner dot is selected. The dot selected in this grid determines what the X and Y coordinates refer to.
You may have the center dot selected and so the X,Y coordinates indicate the coordinates of the sub-view's center relative to the parent.

Related

Adding controller buttons to prompt text?

I want to make a controller button prompt in my game where it says 'Press X to Join', but I want the X to be an actual graphic of the 'X' button, like how it is in many games. How would I go about doing that? Right now I'm just putting a big space in my prompt text and putting a UI Image of the button in it, but I want to know if there's a better way about it.
For the sake of scaling to different resolution sizes, you would wanna scale the anchors of the UI elements correctly and have an appropriate parent-child relation tree in Unity's hierarchy.
The red box is the hierarchy.
The 2 green boxes shows ways of scaling the anchors.
The orange box shows the end result of it.
Anchors in combination with their relative position, allows Unity's UI elements to scale up and down according to screen size.
So for example if you say that an anchor is at 0.25x, that would be that its anchored at 25% of for example the x axis. Same goes if you set it for the y axis, just the vertical instead of horizontal anchoring.
You can use the anchors to adjust a minimum and a maximum anchor which the elements may float within, they may have the same value as well, then it's a fixed anchor point.
To clarify, I suggest that you use a panel to hold 2 text elements and the image with the X, each text element being on the left and right side of the X instead of having spacing inside the UI text elements. To keep correct spacing you then must use anchor points. This way your spacing stays correct despite changing screen and resolution sizes.
Please note that the "left", "top", "right" and "bottom" values are then relative to the anchor points. So if you move "left" 5 pixels, those 5 pixels will be out from the relative anchor point.
Here's the values I used:
My left text is at 0.25x, right text is at 0.8x, image is at 0.5x.
The panel holding the 3 is at 0.2 minimum x to 0.8 max x, same goes for y axis.
The largest parent panel is stretched to max fit in the canvas.

How to allow only X axis rotation of 3D model? (SceneKit)

I've an app that should show a 3D model, at this point all is OK, but this model rotates in all 3D space axis and I need that this model rotate only on X axis (left/right) when user tries rotate the model.
Any idea? I've been stuck here for a while...
Provide your own camera. Don't use the "allows camera control" checkbox in Interface Builder, and leave the view's allowsCameraControl property set to false.
You said "X axis (left/right)". But in SceneKit, the X axis runs left to right, so rotating left/right is a rotation around the Y axis. Add a swipe gesture recognizer to recognize swipes left and right. Use the recognizer's handler to rotate your model around the Y axis as the user's finger slides left and right.
Have a look at SCNTransformConstraint.
You can specify a SCNTransformConstraint as a value in the constraints property of the SCNNode to which the geometry is assigned.

Use auto layout to position button near the center

I'm using auto layout in Xcode and I have some buttons I want to be near the center, but not on it. I know I can use alignment constraints to be exactly in the center, in this menu:
However, I want to not be exactly in the center, lets say 50 pixels up and to the right. How can I do this? I'm sure it's an easy thing to do, but I can't seem to find anything elsewhere on the web.
If you want the center of your button to be 50 points up and to the right of the parent view's center you can use a center x alignment constraint and a center y alignment constraint and set the center x alignment constraint's constant to 50 and the center y alignment constraint's constant to -50.
Create a view with size 50 * 50. Then place this view exactly at the center using auto layout.
Next you need to place your button using vertical space and horizontal space in order to be aligned to the upper right corner of the view.
Finally make the view transparent.

Frames and Bounds (resizing) - Cocoa

I'm having resizing issues, and I think it's because I don't really know enough about frames and bounds.
I have a custom view within a scroll view, which fills the window. When I resize the window, I want the custom view to stay where it is, slowly getting covered/uncovered by the window in the place that the mouse is dragging.
What really happens is the custom view stays anchored to the lower left corner of the scroll view, so that if I make the window shorter, the custom view slides up to keep its lower left corner touching the scroll views corner.
How do I resize the window without moving a particular view?
The frame is the area that the view will occupy within its parent. The bounds is the section of the view that will be drawn within its frame. So 99.99% of the time that the two differ at all, they have the same size but the bounds has a zero origin and the frame has a non-zero origin.
That said, it sounds more like you're confused about the coordinate system. OS X follows the graph paper convention of the origin being in the lower left hand edge of the screen. So your scroll view's origin is in the lower left of the window, which results in that point being the anchor when you resize. The size of the scroll view's frame and bounds changes but the origin doesn't.
Assuming you want the top left to be anchored rather than the bottom left (?), possibly the easiest thing to do would be to subclass NSScrollView and override - setFrame: to do appropriate arithmetic — grab the current documentVisibleRect, work out what's in the top left, allow super to set the new frame then call scrollToPoint appropriately.
If you want to pin your document view to the top left, you can override isFlipped in your document view to return YES. In that case the y coordinate will be flipped and you may need to perform some computation adjustments.
- (BOOL)isFlipped
{
return YES;
}

About cutting of / cropping a uiview

so i have a uiview that is initialized with a frame that has the height and width that is present for the user, i want the user to be able to draw inside this frame but when the user presses a button, i want the view to cut off that extra wasted space so that the frame is only as big as what the user was drawing. I tried to do something like this
CGRect boundbox = CGPathGetBoundingBox([myPath CGPath]);
boundbox.origin.x = self.frame.origin.x;
boundbox.origin.y = self.frame.origin.y;
self.frame = boundbox;
However, this does not remove that extra wasted space, it only resizes the view, so that the drawn content looks smaller than previously. What i would like to do instead is to remove
that "whitespace", i was thinking if it could be possible to scale up the content of the uiview, but im not sure.
To clarify what i mean:
The red border is the area / frame that the user can draw on, the text in the middle is a drawing, when the user presses a button, i want the frame to only encircle the drawing like in figure 2.
Now lets say i have the following scenario, i have drawn a circle on the middle of the screen.
When i then press the button, the scale remains the same but the circle is still in the same position but we have now changed the draw area, so the circle / drawing will look like its cut off like in figure 4.
What i want to do is to move the drawing / bezier path so that it is positioned in the middle of the frame. So that the red area encircles the blue circle.
[EDIT]
Given your drawings. A UIView will not re-position items in it when you change it's frame property (or it's CGRect). In this case you will need to track the items drawn YOURSELF, and then when the button is pressed perform the object translations yourself.
What that means is you will have to find the object that is left most, the object that is topmost, then move all objects left by that amount, and up by that amount so that all objects are (as a grouping) top-left aligned within the view's frame. After this you will need to self recognize which object is the right most touching and which object is the bottom most touching.
NOW, since you have already moved the items left-top, the right most point will define your frame width, and the bottom most point will define your frame height.
IF YOU SO DESIRE, you should be able to zoom in using the properties below after you have done this.
[First Answer]
If I understand your question correctly, you may want to still perform your box frame manipulation, but if you wish to scale you may want to look into the
contentScaleFactor or
contentStretch
properties.
contentScaleFactor should scale both dimensions based upon a singular floating point value (i.e. xWidth * scaleFactor, yHeight * scale factor).
contentStretch is a CGRect which means that it should scale each dimension (axis) separately.

Resources