Rotating an Scaling a UIBezierPath on Autorotate - ios

I'm working on a simple drawing View. For this I add CAShapeLayers with a UIBezierpath to a UIView. My problem is, that on rotation the drawings will not resize. Instead it goes out of bounds.
Basically I try to imitate the behavior of the photo-app where have an image in portrait filling the whole screen and on rotation, it will scale it corresponding to landscape without changing the aspect ration.
I found solutions here which do the rotation at the same position, but I can't find anywhere how to do the scaling.
Thanks

Related

SpriteKit sprites are cropped in portrait orientation

I am having trouble understanding the co-ordinate system in SpriteKit.
The documentation states that a scene size is 1024x768 and the anchor point for the scene is CGPointZero (i.e. the bottom left of the scene). So in Landscape orientation the scene's CGPoint(0,0) is on the bottom left of the view - all good.
However, if the ScaleMode is AspectFill then (as the documentation describes) some of the scene is cropped in portrait orientation - again that makes sense.
Where I am confused is that the cropping appears to be based on an AnchorPoint of CGPoint(0.5,0.5). I.e. a sprite with AnchorPoint CGPoint(0,0) that is drawn at scene position CGPoint(0,0) is cropped in portrait orientation.
My question is: "If the scene's anchor point is CGPoint(0,0) and the ScaleMode is AspectFill, why does this the node at CGPoint(0,0) cropped? Shouldn't it ensure that the bottom left of the scene is always at the bottom left of the view?"
Do not confuse your scene with your view.
Yes, for your scene, the default is bottom left. But scaling applies from the scene to the view. By default, your scene is added to the center of the view.
As far as scaling goes, imagine it being like a balloon that you are going to wrap around a box.
You pull it from all ends evenly till the entire balloon fits around the edges of the box.
Then you take Aspect fill into consideration by taking 2 rectangle boxes, and placing them on the sides so that your box top turns into the desired shape.
This is why (0,0) ends up getting cropped off.
The anchor point applies to what happens inside the scene, not outside.
You would need to change the views anchor point (Not possible that I know of) to achieve what you want, or just shift the scene to the right by the difference of (sceneWidth * scaleFactor) - viewWidth

Rotate coordinate System of an UIView

Im a complete beginner, so please excuse my maybe stupid question.
Im using an UIView "superview" with many children-images to perform a rotation of the images around an arbitrary point:
superview.transform = CGAffineTransformMakeRotation(M_PI/2);
This works perfect, but it seems like this is rotating the coordinate system of the superview as well. This in turn messes up all following steps. I find hundreds of posts and infos concerning rotating images and for mac development there obviously is a method for rotating coordinate systems. I just don't find anything for iOS.
Is there a way to rotate a view without rotating the coord system / how can I rotate the coord system without influencing the images?
Thanks for any help.
it seems like this is rotating the coordinate system of the superview as well
You're right -- rotating the coordinate system of the superview is exactly what you're doing:
superview.transform = CGAffineTransformMakeRotation(M_PI/2);
Is there a way to rotate a view without rotating the coord system
Sure, just set the transform property for your view rather than for its superview. For any view someView, you can say:
someView.transform = CGAffineTransformMakeRotation(M_PI/2);
Note that this actually rotates the view's own coordinate system -- it doesn't rotate the view itself. That means that all your drawing will be rotated, but the view's frame will remain the same. You might need to adjust the frame to account for this. For example, if you have a rectangular view that's normally 100px high and 300px wide, you might want to make it 300px high and 100px wide to ensure that the content is all still visible.
If you're creating a custom view, you can of course refer to instances of that view from inside its own code using self:
self.transform = CGAffineTransformMakeRotation(M_PI/2);

How to apply scale animation on a rectangular UIView which has been rotated?

I have to draw a rectangular shape which appears to grow.
My Approach
1.i made a UIView subclass,say DrwArrow and in its draw rect method i am drawing my shape I am initializing it with the height required.
i am now adding the DrwArrow object on my view and setting its center at the required point and then applying rotation at the required angle.
3.In order to apply scale animation i am setting its rect to 0 height and the scaling it to the required height.
Issue
The approach works fine when the rectangle is either vertical or horizontal but when the angle is not a multiple of 90 then it scales erroneously, I guess because the height of rect changes due to rotation applied..
I need suggestion regarding the approach i should follow.
Before downvoting please gv rsn so that i can improve.

Using panning gesture to animate GCRect resize

I'm drawing a few circles, each filled with an image. When the user pans I'd like to scale/resize the circles. So I called drawRect again and again, redrawing every GCRect until the gesture was completed - of course the animation was very choppy. In my case a UIScrollView doesn't fit the needs, because I don't want to scroll, but to scale the circles while the user is panning.
Is there any way except using OpenGL ES to implement this functionality?
Do you really need custom drawing for this? You can easily clip an image to a circle without drawRect.
Without -drawRect:
Using Core Animation you can set the corner radius of a layer. If all you want is to show an image inside a circle then you can put the image in an image view with a square frame and set the corner radius of the image views layer to half the width of the frame.
Now each time the user drags you can change the bounds and the corner radius of the image views layer. This will make it look like the circle becomes bigger/smaller.
If you require custom drawing
Maybe you are doing some custom shadows or blending that can only be done with Core Graphics. If so, you could apply a scale transform and stretch the image while the user is dragging their finger and only redraw once the finger lifts from the screen. That will be much, much cheaper and is also very easy to implement. Just create a scale transform (CGAffineTransformMakeScale(xScale, yScale);) and set it as the transform on the view with the circle (this will only work if each circle is its own view).
Note: You can still use the same trick (scaling while dragging and then redrawing) if you use the corner radius approach if you require the extra performance.

Move a UIImageView after applying CGAffineTransformRotate behave incorrect

I'm trying to rotate UIImageView for certain degrees with CGAffineTransformMakeRotation() function, but it end with imageView only rotates, but when I moved to another coordinates then imageView stretched or change height and width. I have no clue why this happens.
Here is a code:
double a = atan2(dx,dy);
bowImageView.transform = CGAffineTransformMakeRotation(a);
WHen you apply a transform to a view the geometry changes. If you wish to move that view rotated, the simplest solution is to set the transform back to CGAffineTransformIdentity, move the view and reapply the rotation.
The other way is to work out the movement based on the current rotation.....but I find the first solution easier!

Resources