Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, May 24, 2016

Rotate UIView around its center keeping its size

Rotate UIView around its center keeping its size


I'm trying to rotate an UIView a few radians but after applying the transformation it doesn't look to be keeping its size. What's the proper way to achieve this?

Here's what I'm doing and what I get (Blue box with the arrow is the View I'm trying to rotate -- it should keep same aspect as red box behind):

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)    double rads = DEGREES_TO_RADIANS(240);  CGAffineTransform transform = CGAffineTransformRotate(CGAffineTransformIdentity, rads);  self.arrowView.transform = transform;  

enter image description here

Thanks!

Answer by Abhinav for Rotate UIView around its center keeping its size


Try with this code:

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)    double rads = DEGREES_TO_RADIANS(240);  self.arrowView.layer.transform = CATransform3DMakeRotation(rads, 0, 0, 1);  

Answer by Sauvage for Rotate UIView around its center keeping its size


To my mind you need to calculate the center of your triangle and rotate around this point. Now you rotate the arrow around the center of your square.

See: One step affine transform for rotation around a point?

I Hope it will help you.

Answer by n00bProgrammer for Rotate UIView around its center keeping its size


The CGAffineTransformRotate transformation rotates from an existing affine transform. The fact that you are using CGAffineTransformIdentity might be the issue. You must specify the current transform of your view.

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)  ...  double rads = DEGREES_TO_RADIANS(240);  CGAffineTransform transform = CGAffineTransformRotate(self.arrowView.transform, rads);  self.arrowView.transform = transform;  

Also, you might want to consider:

self.arrowView.transform = CGAffineTransformMakeRotation(rads);  

EDIT : If you can, share what you kind of transformation (animated/inanimate , single/iterative) you want to achieve. I believe there might be a better, optimized way of doing this.

Answer by jrturton for Rotate UIView around its center keeping its size


You're probably hitting a problem with Autolayout. You probably have constraints on the rotated view pinning it to the edges of the superview. When the transform is applied, Autolayout is updating the view's size to still fit within the superview.

You can experiment with different constraints (e.g. pinning the centre of the view to the centre of another view, and pinning the width and height to constant values) or turn Autolayout off for the rotated view, or, if these don't work or don't suit your needs, use a container view which is laid out under Autolayout, and add your rotating view to this, without using Autolayout.

This can only be done in code - you can make individual views subject to Autolayout or not by setting translatesAutoresizingMasksIntoConstraints to NO (Autolayout on) or YES (Autolayout off). You'll need to set the appropriate autoresizing masks if you switch a view from one to the other.

Answer by Shaheen Ghiassy for Rotate UIView around its center keeping its size


I avoid using macros unless necessary. This works perfectly well

float degrees = 20; //the value in degrees  view.transform = CGAffineTransformMakeRotation(degrees * M_PI/180);  

Answer by Luca Davanzo for Rotate UIView around its center keeping its size


Swift + extension are your friends!

// MARK: - UIView Extension -    extension UIView {        /**       Rotate a view by specified degrees         - parameter angle: angle in degrees       */      func rotate(angle angle: CGFloat) {          let radians = angle / 180.0 * CGFloat(M_PI)          let rotation = CGAffineTransformRotate(self.transform, radians);          self.transform = rotation      }    }  

In this way, anywhere in your code:

let view = UIView(frame: CGRectMake(0, 0, 100, 100))  view.backgroundColor = UIcolor.redColor()  view.rotate(angle: 90)  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.