About cutting of / cropping a uiview - xcode

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.

Related

Forcing Margins to UIImageView in UICollectionView Cell

I'm looking for a way to force a UIImageView inside a collection view cell to maintain 8 points margins, similar to this picture:
It doesn't work all the time, sometimes it crosses the right edge like this:
The image view is set to AspectFill because I want it to fill the space without distorting it. Here is how it looks in Xcode:
I want to force 8 points margin in all sides except for the bottom.

Custom Progress Indicator - Animate subviews color overlay left to right

I have a container UIView that contains many columns/subviews.
I want to create a color fill animation on the container UIView, left to right as demonstrated on the image below, so it gradually changes the colors of only the subviews.
How to achieve this?
Im guessing i would have to create a mask and move it over this container UIView?
But then how to achieve that the mask only applies to the subviews?
EDIT:
this is a progress indicator for a playing audio file, so it needs to indicate the current time position precisely.
Also i must be able to have the shapes in different sizes as they represent audio waves
I would do it the opposite to the way you describe it. I would draw your audio wave shapes as a mask, thus revealing whatever is behind them. It is then trivial to position / animate a colored rectangle rightwards behind the mask to indicate the progress through the music:
In other words, the color is not an overlay but an underlay.

NSView - under what conditions may the bounds and frame *size* be inequal?

I know that frame is a view's frame relative to it's parent, and bounds is the view's internal bounds (with origin always [0, 0] except in the case of scroll views?).
However, I'm unclear under what conditions the frame and bounds size may differ, if at all. Is there official Apple documentation stating whether frame.size = bounds.size or frame.size ?= bounds.size?
You can do whatever you want with the bounds. Imagine that your view is a painting, which you can only view through a camera. Moving the origin will change the portion of the painting which you can currently see by moving the camera around. Shrinking the size zooms in, so that less of the painting is visible, but it appears larger. Expanding the size zooms out, so that more of the painting is visible, but it appears smaller.
From the documentation for bounds:
By default, the origin of the returned rectangle is (0, 0) and its size matches the size of the receiver’s frame rectangle (measured in points)....
If you explicitly change the origin or size of the bounds rectangle, this method does not return the default rectangle and instead returns the rectangle you set. If you add a rotation factor to the view, however, that factor is also reflected in the returned bounds rectangle.
And from the documentation for setBounds::
The bounds rectangle determines the origin and scale of the receiver’s coordinate system within its frame rectangle....
After calling this method, NSView creates an internal transform (or appends these changes to an existing internal transform) to convert from frame coordinates to bounds coordinates in your view. As long as the width-to-height ratio of the two coordinate systems remains the same, your content appears normal. If the ratios differ, your content may appear skewed.

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;
}

How do I make a textview auto aline to textview?

How can I have a textview auto-align to an image?
What I mean is, when I change an image from a square to a triangle, how do I make the textview fit inside that triangle automatically?
Check out my "Quick Notes!!" app on the app store for reference. I am using Xcode 4.3.
Chances are you're going to have to do a bit of work to do what you want. I'm guessing you have a background image that changes from a square to a triangle and you want the textbox to remain inside of the shape. Since the imageview hosting the picture is still a rectangular shape the imageview dimensions don't actually change so you'd have to calculate or know the size of your triangle and scale the text box programatically based on a flag of whether or not you are displaying the triangle.
EDIT: Actually this is much simpler than i thought initially. Just position the textbox where you want it with what size in the .xib file and record the position and size. Then you simply check when you are displaying your triangle image and change the position and size of your textbox to what you had previously recorded.

Resources