How to rotate object only one side - ios

i have already rotate success from center point.
But its not rotate from one side.
for ex,
When i have select point A & rotate then its rotate from Only A side,not from Point B side.
When i have select point B & rotate then its rotate from Only B side,not from Point A side.
I have already set anchor point but its not work.
Code Like that:
CGFloat angle = [self pointPairToBearingDegrees:frspnt secondPoint:lastpnt];
CGAffineTransform newtrans = CGAffineTransformMakeRotation( ( angle * M_PI ) / 180 );
[sender.superview layer].anchorPoint = CGPointMake(1.0, 1.1);
sender.superview.transform = CGAffineTransformConcat(sender.superview.transform, newtrans);

Related

Rotating image like in standard feature ios objective c

How can I rotate an image similar to how it's done in the standard gallery feature? The standard gallery has wheel which the user can tap with one finger and swipe from left to right and turn image to 45 degrees.
Here a screenshot of what I mean:
Try this code for rotation.
CGAffineTransform newTransform = CGAffineTransformMakeRotation((CGFloat)(angel));
self.imageView.transform = newTransform;
try this code for rotation.
CGFloat degrees = 45.0f; //the value in degrees
CGFloat radians = degrees * M_PI/180;
imageView.transform = CGAffineTransformMakeRotation(radians);
I think you need single finger rotate.
Here is library you can go with:
https://github.com/zedoul/ZDStickerView

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

UIView disappears after applying CATransform3DRotate

How to determine whether value for angle of rotation of UIView is perpendicular to Y-axis or to restrict the angle of rotation to only quarter of the circle instead of whole 360 rotation.
I have a UIView with the below applied CATransformation,.
t = CATransform3DIdentity;
//Add the perspective!!!
t.m34 = 1.0/ 300;
t = CATransform3DRotate(t, 45.0f * M_PI / 180.0f, 0, 1, 0);
self.containerView.layer.sublayerTransform = t;
like in this link1
and changing the rotation angles based on UIPanGesture in the space outside of the rotated UIView with below code
- (void)handleRevealGestureStateChangedWithRecognizer:(UIPanGestureRecognizer *)recognizer{
//Calculating percent of translation.
CGPoint translation = [recognizer translationInView:recognizer.view.superview];
CGFloat rotationpercent = translation.x / recognizer.view.frame.size.width;
[self rotate:rotationpercent]; //calling method to transform based on translation value
}
- (void)rotate:(CGFloat)rotateDegress{
CGFloat angle = M_PI * rotateDegress;
t = CATransform3DRotate(t, angle, 0, 1, 0);
self.containerView.layer.sublayerTransform = t;
}
this rotates the view and for certain angles the view is either perpendicular to Y-axis on further it goes beyond window to rotate in full 360 degrees like shown in this link2.
Hence is it possible to determine if the angle is exceeding 90 degree so that rotation is disabled.
Thanks
Your code is using angles in degrees, which is fine (since you convert to radians before applying a rotation.)
Just put code in your gesture recognizer that limits the angles to a range of 0 to ~85 degrees. (Experiment with the upper limit to see how close to 90% you can get before the page is too thin to maintain the illusion of depth.) Problem solved.

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.

Animation moves image

I am trying to create my first iPhone application. The application is simply a arrow pointing at north.
I have so far added a image of a arrow, and created a animation via the following code:
- (void)setDirection:(float)degree {
float rad = M_PI * (float)degree / 180.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:10];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
//[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
DirectionArrow.transform = CGAffineTransformMakeRotation(rad);
[UIView commitAnimations];
}
My problem is that the arrow "moves" before it rotates, each time I call the method. I have tried varius things to center the rotation angle, but without any luck.
I want the arrow (image) to rotate around its own axis.
This is because your view is rotating around (0, 0), which is the top-left corner of your view. You'd want to rotate around the center of the arrow instead.
To do so, you'll have to build a transform that does the following:
Translates the arrow such that it's center is at (0, 0).
Rotates the view in rad degrees.
Translates the arrow back (the inverse transform of (1)).
It should be something like (up to flip of t1 and t3):
CGFloat h = view.bounds.size.height;
CGFloat w = view.bounds.size.width;
CGAffineTransform t1 = CGAffineTransformMakeTranslation(-w/2, -h/2);
CGAffineTransform t2 = CGAffineTransformMakeRotation(rad);
CGAffineTransform t3 = CGAffineTransformMakeTranslation(w/2, h/2);
CGAffineTransform t = CGAffineTransformConcat(CGAffineTransformConcat(t3, t2), t1);
DirectionArrow.transform = t;

Resources