Move, Scale, Rotate UIView to specific corner Points - ios

I have a the corner points of a rectangle on the screen and want to fit an UIImageView in this rectangle.
I want it like in this picture:
What's the best way to achieve this?

I think black box is UIImageView and you're trying it to place in screen as in the picture.
You can put the imageview in storyboard, in xib or in code to right side of screen. Then rotate it. For example below code rotates the imageview 20 degress.
float degrees = 20; //the value in degrees
imageView.transform = CGAffineTransformMakeRotation(degrees * M_PI/180);

Related

Rotating a view using another view's center as the anchor point

I have two image views. The first is the blueish arrow, and the second is the white circle, with a black dot drawn to represent the center of the circle.
I'm trying to rotate the arrow so it's anchor point is the black dot in the picture like this
Right now I'm setting the anchor point of the arrow's layer to a point calculated like this
CGFloat y = _userImageViewContainer.center.y - CGRectGetMinY(_directionArrowView.frame);
CGFloat x = _userImageViewContainer.center.x - CGRectGetMinX(_directionArrowView.frame);
CGFloat yOff = y / CGRectGetHeight(_directionArrowView.frame);
CGFloat xOff = x / CGRectGetWidth(_directionArrowView.frame);
_directionArrowView.center = _userImageViewContainer.center;
CGPoint anchor = CGPointMake(xOff, yOff);
NSLog(#"anchor: %#", NSStringFromCGPoint(anchor));
_directionArrowView.layer.anchorPoint = anchor;
Since the anchor point is set as a percentage of the view, i.e. the coords for the center are (.5, .5), I'm calculating the percentage of the height in arrow's frame where the black dot falls. But my math, even after working out by hand, keeps resulting in .5, which isn't right because it's further than half way down when the arrow is in the original position (vertical, with the point up).
I'm rotating based on the user's compass heading
CLHeading *heading = [notif object];
// update direction of arrow
CGFloat degrees = [self p_calculateAngleBetween:[PULAccount currentUser].location.coordinate
and:_user.location.coordinate];
_directionArrowView.transform = CGAffineTransformMakeRotation((degrees - heading.trueHeading) * M_PI / 180);
The rotation is correct, it's just the anchor point that's not working right. Any ideas of how to accomplish this?
I've always found the anchor point stuff flaky, especially with rotation. I'd try something like this.
CGPoint convertedCenter = [_directionArrowView convertPoint:_userImageViewContainer.center fromView:_userImageViewContainer ];
CGSize offset = CGSizeMake(_directionArrowView.center.x - convertedCenter.x, _directionArrowView.center.y - convertedCenter.y);
// I may have that backwards, try the one below if it offsets the rotation in the wrong direction..
// CGSize offset = CGSizeMake(convertedCenter.x -_directionArrowView.center.x , convertedCenter.y - _directionArrowView.center.y);
CGFloat rotation = 0; //get your angle (radians)
CGAffineTransform tr = CGAffineTransformMakeTranslation(-offset.width, -offset.height);
tr = CGAffineTransformConcat(tr, CGAffineTransformMakeRotation(rotation) );
tr = CGAffineTransformConcat(tr, CGAffineTransformMakeTranslation(offset.width, offset.height) );
[_directionArrowView setTransform:tr];
NB. the transform property on UIView is animatable, so you could put that last line there in an animation block if desired..
Maybe better use much easier solution - make arrow image size bigger, and square. So the black point will be in center of image.
Please compare attached images and you understand what I'm talking about
New image with black dot in center
Old image with shifted dot
Now you can easy use standard anchor point (0.5, 0.5) to rotate edited image

Rotate UIImageView from Particular side - iOS

I want to rotate image (UIImageView) from particular side (for ex. Right Side).
I know through :-
float degrees = 20; //the value in degrees
imageView.transform = CGAffineTransformMakeRotation(degrees * M_PI/180);
but it rotates from center.
I want to rotate my UIImageView from one particular side (i.e. Left side will be at its place, and right side will change its position).
Thanks in advance.
Try to use anchorPoint property.
imageView.layer.anchorPoint = CGPointMake(0, 0);//Top left angle
float degrees = 20; //the value in degrees
imageView.transform = CGAffineTransformMakeRotation(degrees * M_PI/180);
Do not forget to add QuartzCore framework.
Rotate from center and move UIImageView where you will. Result will be the same as rotating from right edge.

Need to find final location of moveable UIImageView in iOS

I have a UIImageView that I am able to move on my screen. However, what I would like to know is the center point of the UIImageView after the user has stopped moving it. My UIImageView is located inside my main view.
I have tried using:
CGPoint centerPoint = _imageView.center;
but unfortunately, this value remains the same regardless of where I move the UIImageView on the screen. How can I determine the center point of the UIImageView that is dynamic due to the fact that the user will be moving the UIImageView on the screen?
You can do
CGRect frame = _imageView.frame;
And then do some math with the x coord, y coord, height, and width to get the center with respect to your main view.

Having a portrait label in a landscape view

I've created a .xib with a landscape orientation UIView.
The problem I'm trying to solve is that I want a UILabel running vertically along the side with text reading from bottom of the view to the top, but I can't figure out how to do that. Is that possible?
Image to show what I mean
You need to rotate and then translate it to put it where you want it. Rotation happens around the center of the label which is why you then need to translate it.
If you laid out the text label with the upper left corner where you wanted it to end up (i.e. the displayed label looks like it rotates around the upper left corner point of the label), you'd use code something like:
- (void)viewDidLayoutSubviews {
CGAffineTransform transform = CGAffineTransformMakeRotation(3 * M_PI_2);
CGAffineTransform transform2 = CGAffineTransformConcat(transform, CGAffineTransformMakeTranslation(floor(-self.label.bounds.size.width / 2), floor(-self.label.bounds.size.width / 2)));
self.label.transform = transform2;
}
You might need to adjust the translation values slightly to get what you want, but you definitely want them to be integers (which I've done with floor) so the label is crisp.
Of course you can. You need to do a transform.
yourlabel.transform = CGAffineTransformMakeRotation(-M_PI/2);

Rotating UIImageView

I want to rotate a UIImageView, and I am using this code:
-(IBAction)rotateImageView:(id)sender{
photoView.transform = CGAffineTransformMakeRotation(M_PI); //rotation in radians
}
The code works. when I press the button, the image rotates 180 degrees, but if I press it again it doesn't rotate back to its original position, but it stays still. why??
Because you are applying the same transform to the view again, without taking into account any existing transform.
Try this:
photoView.transform = CGAffineTransformRotate(photoView.transform,M_PI);
you have to rotate the imageView from the current rotation to make it original
use this code
CGAffineTransformRotate(photoView.transform, M_PI);

Resources