How to get slider value with subview rotation angle - ios

Hi I used UISlider and rotate an image/subview based on slider value. Slider minimum value is zero and maximum value is 360. I had multiple views. With slider I am rotating the Image perfectly.
UIView *subView = [self.view viewWithTag:100];
subView.transform = CGAffineTransformMakeRotation(slider.value * 2*M_PI / slider.maximumValue);
I selected another subview and next again selected the first view. So based on view angle I need to set slider value. But I am getting wrong.
CGAffineTransform transform = subView.transform;
CGFloat angle = atan2f(transform.b, transform.a);
[slider setValue:(angle / ((2*M_PI)/ 360)) animated:true];
What I did Wrong?
Thanks in Advance.

After some struggle I got the answer. I am getting negative angle after 90. So if I got negative angle I calculated the differences between required angle and the current angle.
CGFloat angle = [(NSNumber *)[subView valueForKeyPath:#"layer.transform.rotation.z"] floatValue];
if (angle < 0) {
angle = 6.28 + angle;
}
[slider setValue:(angle*360 / (2*M_PI)) animated:true];

Related

CGAffineTransformRotate set to its initial state when want to increase view size with UISlider

I am rotating my view using UIRotationGestureRecognizer, But after taht when i want to increase or decrease my view size using UISlider. My view angle first setting to its initial state the it is increasing or decreasing my view size. My requirement is if my view rotated by some angle it still rotated after performing UISlider operation.
My Rotation Gesture Code:
gesture.view.transform = CGAffineTransformRotate(gesture.view.transform, gesture.rotation);
gesture.rotation = 0.0;
My UISlider Code
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, slider.value * 2, slider.value * 2);
If you want to keep the rotation, you'll have to store that CGAffineTransform and then concatinate it with your scale transform. You can use CGAffineTransformConcat for that (https://developer.apple.com/documentation/coregraphics/1455996-cgaffinetransformconcat?language=objc). Something like this:
CGAffineTransform scale = CGAffineTransformScale(CGAffineTransformIdentity, slider.value * 2, slider.value * 2);
myView.transform = CGAffineTransformConcat(savedRotationTransform, scale);
where savedRotationTransform is CGAffineTransformRotate(gesture.view.transform, gesture.rotation); saved from that other step. It might be easier to encapsulate the whole thing in your UIView subclass.

How should I do to get a UIView instance rotation angle when it is in rotation animation status?

I want to do a demo like the game called aa. So I need to get the real-time angle about the view rotation. The following codes just can obtain final angel:
CGFloat rotationAngle = atan2f(_view.transform.b, _view.transform.a);
CGFloat degreeAngle = rotationAngle * (180 / M_PI);
In my mind, if I can know coordinates of the point insert pin during the central view is rotation, I maybe finish this demo. For example,this point can make like this:
So I want to know how to rotate the view of the current has obtained is how many degrees, or other way to realize game aa.
After some times testing, these codes can solve this problem:
- (CGFloat)transformAngleWithRotationDirection:(BOOL)clockwise {
return clockwise ? [self transformRotationAngle] : 2.0f * M_PI - [self transformRotationAngle];
}
- (CGFloat)transformRotationAngle {
CGFloat degreeAngle = - atan2f(self.presentationLayer.transform.m21, self.presentationLayer.transform.m22);
if (degreeAngle < 0.0f) {
degreeAngle += (2.0f * M_PI);
}
return degreeAngle;
}

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.

Rotation UIImageView around An anchorPoint

i want to rotate multiple UIImageView around anchor point and give them a little rotation so that could be something like this
https://www.google.com.eg/search?q=cards.&um=1&ie=UTF-8&hl=en&tbm=isch&source=og&sa=N&tab=wi&ei=zzkjUoXFL5GWswbrrIGgBw&biw=1024&bih=615&sei=0TkjUu2wHcmntAbC54Bo#facrc=&imgdii=&imgrc=BdArJtkexKgyQM%3A%3BnKsOa4ZSkOOFcM%3Bhttp%253A%252F%252Fdailywicca.com%252Fwp-content%252Fuploads%252F2011%252F10%252Fplaying-cards1.jpg%3Bhttp%253A%252F%252Fdailywicca.com%252F2011%252F10%252F08%252Freading-with-a-plain-deck-of-cards%252F%3B1920%3B1200
this is my code i think it should work but wear thing happened
int angel=0;
for (int i=0; i<6; i++)
{
UIImage *image=[UIImage imageNamed:#"images.jpeg"];
UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
[self.view addSubview:imageView];
[imageView setFrame:CGRectMake(0, 0, 80, 80)];
[imageView setCenter:CGPointMake(150, 150)];
imageView.layer.anchorPoint = CGPointMake(1.0, 1.0);
imageView.transform = CGAffineTransformMakeRotation(angel);
angel+=3;
}
i think they all should be in the same side as iam increasing angle with 3 , how to achieve this ?
This is due to the fact that CGAffineTransformMakeRotation() takes radians as input, not degrees. Now, you could easily convert from degrees to radians using a macro like this:
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
Or you could just use smaller input values. In radians, 360 degrees is equal to 2 * π ~ 6.28, so instead of ranging your values assuming 0-360, you should assume 0-2π. When you increase the value by 3, you're effectively increasing it by just under 180 degrees, which is why you're seeing what you are.
Overall, try using angel += 0.052539;. You'll need to change angel from an int to a float as #Kyle W. pointed out
I'm not entirely sure I understood your question, but try this:
Change the angle from an int to a float.
float angel = 0;
Change the increment for each iteration of the loop.
angel += 0.3;
Also, I think you mean "angle" not "angel" ;)

IOS: rotate an uiimageview

in my app I have an imageview, its name is arrow and I rotate it of 180 degrees in this way:
arrow.transform = CGAffineTransformMakeRotation(M_PI);
it work fine, but now I want that this imageview return in the original position; what's the value taht I should set to obtain it?
To make it return to it's original position, just do this:
arrow.transform = CGAffineTransformMakeRotation(0);
In degrees, 0 radians is 0 degrees, so it should return to its original position.
Try setting the transform property back to the identity matrix. i.e. CGAffineTransformIdentity
Swift3:
arrow.transform = CGAffineTransform(rotationAngle: rotation);
where rotation is the angle to rotate in radians.

Resources