Bounds vs Frame when making a custom view and initializing it - ios

I'm having some difficulties understanding the difference, and when to use what.
I know the textbook definitions. I have also searched a lot regarding this very topic. Some answers here on SO were helpful to some extent, but I feel I still don't understand this properly.
Let's say I have a aCustomView.m, and when I place UI elements within that view, I use bounds, which makes sense because it's in it's own coordinate system, but, when I initialize the view using initWithFrame: in my view-controller, should I use self.view.frame or self.view.bounds? Both would work, but with different results.
I really want to understand this, so any help would be appreciated.

The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).
The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.
So the difference is only a question of representation.

Related

PaintCode, How to add a frame around a portion of a vector and have it dynamically resize?

I've imported a vector layer from a psd into paint code v1, I'm trying to create a background image and make it universal.
I can't seem to add a frame around the vector, to complicate matters, I only need a portion, the center, of the layer. (The design is based around a circle, it has lines drawn towards the center of the circle.)
I can’t seem to add a frame to dynamically resize the part I need.
I found this http://www.raywenderlich.com/36341/paintcode-tutorial-dynamic-buttons the part about frame ans groups doesn't help me....
When I add a click frame and drag it around the area I need, it's at the same level as the vector layer. I've also tried adding a group around both, but that doesn't seem to obey the frame size either.
I’ve looked through the tutorials and googled adding a frame, but I can’t seem to achieve what I need.
EDIT
A frame is supposed to be at the same level as the vectors you're working with.
All you do then is set the resize rules of your vectors. There is a little rectangle in the frame's parameters interface with straight arrows and springs that you can modify to fit your wishes.
I think I also remember a checkbox setting to resize only what's inside the frame.
Now I haven't used PaintCode for a while, but if this doesn't help you, there probably is a problem with your vector layer.
I don't know if this information helps.
But if you resizing doesn't work as you expected. Look carefully at the transformation box (the one with the springs attached). When you have put a frame around your object. The middle dot in this box should be blue instead of green. if t's green, you may have a problem with the originating point of your objects and then the resizing may not work as you expected.

Is it possible to align views on a path perimeter?

I want to achieve the following by code:
Based on a circular path or arc, I need to place other views evenly around the perimeter.
Is this possible without hardcoding the frames? If so, how?
In addition to that, can I make a view to follow a path ? if so, how?
Update:
I found this answer helpful, but it seems to use a lot of code though...
Place images along a bezier path
It's easy. A view is a layer.
A layer is positioned by its anchor point. So place the layer's anchor point at the center of the circular path, and define the anchor point at a sufficient size to move the layer away from the center to lie on the desired radius.
Moreover, a layer is rotated around around its anchor point, so now just apply a rotation transform in an increment of nths of a circle, where, n is the number of views.
As you can see, I can easily draw your sub-circles evenly spaced out, given any number of desired sub-circles:

What is the relationship between a UIView's frame origin and its center?

Say I'm creating an animation, and I want to move the center of a UIView to a certain CGPoint I have. I know what I want the center to be, just the aforementioned CGPoint, and I know the width and height that I want, but when I go to create the frame for my UIView I have no idea what to set for the x and y for the origin.
Should I just be setting 0, 0 or anything really? Does it matter?
Should I just be thinking as setting center as a different method of setting the origin? Makes me wish CGRect had an instantiator with center as an option.
There has been a previous question addressing a somewhat similar question, but it addresses them as three separate entities, rather than how to deal with center when creating the view.
Yes, go ahead and set the initial frame's origin to anything, since subsequently setting the center will effectively move this origin anyway. It is not uncommon to initialise views with a frame of CGRectZero when it is going to be fully configured later.
frame is actually just a calculated property based on the view's center and bounds, and so all three are intrinsically linked when it comes to view layout. The UIView Class Reference has this to say about it:
The geometry of a view is defined by its frame, bounds, and center
properties. The frame defines the origin and dimensions of the view in
the coordinate system of its superview and is commonly used during
layout to adjust the size or position of the view. The center property
can be used to adjust the position of the view without changing its
size. The bounds defines the internal dimensions of the view as it
sees them and is used almost exclusively in custom drawing code. The
size portion of the frame and bounds rectangles are coupled together
so that changing the size of either rectangle updates the size of
both.

iOS Quartz 2D How to expand layer beyond screen frame

Here is a picture:
Is there a way to expand layer (the area where you draw), to be larger.
At first i thought that (0,0) is the center, but it seems that it is a starting point for layer.
I was planning to draw content at (0,0) then to translate it as necessary.
Or, must i draw whatever i have in center of layer (width/2, height/2) and then translate as necessary?
EDIT_01:
The offset of layer is made by panning gesture recognizer. That is on purpose, since app must have panning of content.
Answers
At first i thought that (0,0) is the center, but it seems that it is a starting point for layer
(0,0) in iOS begins at the top left of the layer, and gets greater going right and down. In OS X, (0,0) begins in the bottom left, and gets greater going right and up.
Is there a way to expand layer (the area where you draw), to be larger
The question I have is, do you need to? This can have performance issues, as you're rendering and storing drawing data when you don't really need to, causing more memory to be used. It looks like your layer is filling the screen correctly, it's just translated incorrectly. If you move it to the correct place, you shouldn't need to expand the layer off screen.
This doesn't mean you can't translate other layers off screen, of course you can. But really you don't want to expand the size of your drawing area to expand off screen.
Must i draw whatever i have in center of layer (width/2, height/2) and then translate as necessary?
If you want them drawn into the center, then yes. You can create your own method to translate your own co-ordinate space into the co-ordinate space of the layer, to make this easier for you.
Documentation
I'd suggest reading this, to get a greater grasp of the geometry workings of CALayer:
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/Layers.html#//apple_ref/doc/uid/TP40006082-SW1
...and this to get an understanding of drawing techniques:
https://developer.apple.com/library/ios/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_layers/dq_layers.html

Why is there an frame rectangle and an bounds rectangle in an UIView?

Well although it's late in the dark night, I don't get it why there are two different rectangles: frame and bounds.
Like I understand it, one single rectangle would have been just enough to do everything. Positioning the View itself relative to another coordinate system, and then clipping it's content to a specified size. What else would you do with two rectangles? And how do they interact with each other?
Does anyone have a good explanation? The one from the Apple docs with the kid holding the fruit is not very good for understanding.
Here's the cheatsheet:
frame is where the view is (with respect to the superview)
bounds is where the view is allowed to draw (with respect to itself)
Some more clarification:
If you are positioning the view in its superview, you almost always change the frame origin.
If you are clipping where the UIView is drawing, you almost always modify its bounds.
Note that you are allowed to have bounds that is bigger than the frame. That is, you can draw "outside the lines" of where you are.
Frame is in the superview's coordinate system, bounds is in the view's coordinate system. From my perspective, it is a convenience to have both. Frame seems to be the more useful of the two, unless there is some case I am unaware of where a subview can have a completely different coordinate system (e.g. pixels scaled differently) than the superview.
I've been having troubles with bounds lately and have done some experimentation. The bounds property does limit where a UIView can draw, but does not limit its subviews. The other thing bounds controls is touch event dispatching. A view will not, as far a I can tell, receive touch events that are outside its bounds. Furthermore, any subview that outside of the parent view's bounds will also not receive touch events. In these situations, you have to pretty meticulously update the bounds of the container view as the size and position of its subviews change. Everything will always draw fine (because subviews aren't clipped by the bounds of their parent) but touches won't be received.
(This really should be a reply to an earlier post, but since I can't reply yet, it's stuck here...)

Resources