Right now this code will only rotate my image 90 degrees. I can not figure out how to rotate it 270 degrees.
transform = transform.rotated(by: CGFloat(Double.pi))
Start with some basic math.
360° = 2π. 180° = π. 90° = π/2. 270° = 3π/2.
This means your posted code is rotating 180 degrees.
Use Double.pi/2 for 90° and 3*Double.pi/2 for 270°.
Related
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
I have a UIView in my view. Right now I have a button moving the UIView on it's y axis. Here is my code
CGRect frame2 = self.test22.frame;
frame2.origin.y -= 1.f/[UIScreen mainScreen].scale; //however many pixels to the right..
self.test22.frame = frame2;
But how can I rotate it? Except using the x or y axis. I want to be able to move the UIView by rotation by 1.f.
To Rotate the UIView create the object of UIView (*view) and make transform on it
Lets to rotate the view 90 degree use below code
CGAffineTransform r_transform = CGAffineTransformIdentity;
r_transform = CGAffineTransformRotate(r_transform,DegreeYoRadians(90));
view.transform = r_transform;
Welcome to SO. You want to look at the UIView method animateWithDuration, and the view's transform property. You'd apply a rotation transform to the view in your animation block. Note that if you change a view's transform, you should not read or write the frame property. Instead use the center property to change the position and the bounds.size property if you need to change the size.
Your code might look like this:
[UIView animateWithDuration: .25
animations: ^
{
self.test22.transform = CGAffineTransformMakeRotation(M_PI/2);
CGPoint center = self.test22.center;
center.y -= 1;
self.test22.center = center;
}
];
EDIT:
I have no idea what you mean when you say "I want to be able to move the UIView by rotation by 1.f." Rotating a view is not moving it, it's rotating it. And rotation is expressed as an angle, not a value like "1". Furthermore, the angle is in radians, where π is 180 degrees, π/2 is 90 degrees, and 2π is a full-circle rotation (or no change, since it puts the rotation back at it's original value.) You have to think in terms of fractions of 2π when you do rotations. Or you can use degrees and convert to radians, where:
degrees = radians * 180/π
Also, your code that moves the view will only change the view's position by half a point on retina displays which doesn't make much sense.
I have a large UIScrollView canvas and I want completely rotate this scrollView. For this I try:
scrollView.transform = CGAffineTransformMakeRotation(90.0)
but in this case I get:
it's not 90 deg. And in my opinion it rotate in 3D. How can I rotate it properly?
CGAffineTransformMakeRotation takes in radians, not degrees.
Try this:
import Darwin
scrollView.transform = CGAffineTransformMakeRotation(M_PI / 2.0)
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.
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.