Rotation UIImageView around An anchorPoint - ios

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" ;)

Related

How to get slider value with subview rotation angle

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

IOS/Objective-C:Animation of Multiple CGAffineTransformations

I am trying to perform an animation that does three things at once: translates, rotates and changes the size of an image.
I can do two at once, translate and size. However, when I add in rotation at the end of the following code, it is ignored. And if I place it at the beginning of the code, the size change is ignored. I've read that you can do a composite transition with view.transform, however, I have not been able to get that to work.
Here is my current code:
CGPoint destPoint = CGPointMake(-100,-50);
float radians =[self Degrees2Radians:-35];
[UIView animateWithDuration:2
animations:^{
//TRANSLATE
self.imageView.center = CGPointMake(self.imageView.center.x + destPoint.x, self.imageView.center.y + destPoint.y);
//ROTATE
self.imageView.transform = CGAffineTransformMakeRotation(radians);
//SCALE
self.imageView.transform = CGAffineTransformMakeScale(0.2, 0.2); // here the final size will be 20%
}
completion:nil
];
}
Can anyone recommend way to get all three things to occur simultaneously.
Here is some code for swift that uses the transform property of the view, but I have not been able to find the equivalent in Objective-C.
view.transform= CGAffineTransform(scaleX: 1.5, y: 1.5)
view.transform = view.transform.rotated(by angle: CGFloat(45 * M_PI / 180))
Thanks in advance for any suggestions.
You can use CGAffineTransformRotate function on the existing transform to apply a rotation. You can also use CGAffineTransformTranslate and CGAffineTransformScale to apply translation and scaling. Please note that, order of the operations matter.
For example if you have an existing transform myTransform you can rotate it like:
myTransform = CGAffineTransformRotate(myTransform, M_PI / 2);
The operation does not affect the input variable, instead, it returns a new transform so make sure you use the return value of the function. That's why I started the line with myTransform = ....
More information is available at https://developer.apple.com/documentation/coregraphics/cgaffinetransform-rb5?language=objc.

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.

Pentagon with UISLiders as circumradii

I want to draw a pentagon in iOS with UISliders as the circumradii (a set of five UISliders going in different directions and originating from the center of a pentagon). Currently I have rotated five UISliders using a common anchorPoint (:setAnchorPoint), but they dont seem to originate from a common point.
How can I go about this? Do I need to start working on a custom control? What more from Quartz2D can I use?
Some specific indicators would be useful (please don't just say "Use Quartz2D")
Here is what I have achieved Imgur
And here is what I want to achieve Imgur
I defined the various sliders as an IBOutletCollection, so I can access them as an array in the controller. Then I run the following code in viewWillAppear:
CGPoint viewCenter = self.view.center;
for (NSInteger i = 0, count = self.sliders.count; i < count; ++i) {
UISlider *slider = self.sliders[i];
CGFloat angle = 2.0f * M_PI / count;
CALayer *layer = slider.layer;
layer.anchorPoint = CGPointMake(0.0f, 0.5f);
layer.position = viewCenter;
layer.transform = CATransform3DMakeRotation(i * angle - M_PI_2, 0.0f, 0.0f, 1.0f);
}
Here's a screenshot of the result:
Is this that you want?
I would try just to turn the sliders. The following code should help:
firstSlider.transform=CGAffineTransformRotate(firstSlider.transform,72.0/180*M_PI);
secondSlider.transform=CGAffineTransformRotate(secondSlider.transform,144.0/180*M_PI);
//and so on
The 72.0/180*M_PI is transformation of degrees to radians.
Hope it helps.
P.S. don't have a mac nearby to check if it works

How do I move the image based on data it receives a variable with IOS?

I'm starting to program in IOS with Xcode, my idea is to carry a watch with hands that mark the altitude according to the GPS data received from the Iphone. I have the variable that returns me the height in meters, how do I do that to interpret it clockwise?
Set the view.transform property based on the distance.
You'll need to convert the distance into radians. Here's some code
#define METERS_IN_A_CIRCLE 100.0f // change this to get a different scale
CGFloat distance = 40.0f; // the distance in meters - set this to your variable value
CGFloat angle = (M_PI * 2.0f) / METERS_IN_A_CIRCLE * distance;
view.transform = CGAffineTransformMakeRotation(angle);
If it's turning the wrong way, use -angle in the transform instead. If it's off by 1/4 of a circle, subtract M_PI_2 from the angle.
For your watch hand to look right, the centre of the hand image will need to be in the centre of the imageView, so you may need to leave a lot of empty white-space in the image, but don't worry it won't affect performance significantly.

Resources