Rotate a Uiview Around Its Center But Several Times

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.

How to rotate a UIView around it's proper center without distortion / skew

The problem was that I was setting the transform more than once per animation frame. This was causing a compound effect with different values. You need to organise your variables so that you only set the transform once during the property modifications.

Rotating a UIView around a center point without rotating the view itself

This is pretty easy to do in Core Animation. I've used some pretty boring static values for this example, so obviously you'll want to make some modifications. But this will show you how to move a view along a circular UIBezierPath.

UIView *view = [UIView new];
[view setBackgroundColor:[UIColor redColor]];
[view setBounds:CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];
[view setCenter:[self pointAroundCircumferenceFromCenter:self.view.center withRadius:140.0f andAngle:0.0f]];
[self.view addSubview:view];

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(CGRectGetMidX(self.view.frame) - 140.0f, CGRectGetMidY(self.view.frame) - 140.0f, 280.0f, 280.0f)];

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.duration = 5.0;

pathAnimation.path = [path CGPath];

[view.layer addAnimation:pathAnimation forKey:@"curveAnimation"];

And a basic function to generate the initial center of the view.

- (CGPoint)pointAroundCircumferenceFromCenter:(CGPoint)center withRadius:(CGFloat)radius andAngle:(CGFloat)theta
{
CGPoint point = CGPointZero;
point.x = center.x + radius * cosf(theta);
point.y = center.y + radius * sinf(theta);

return point;
}

How can I rotate and move a UIView at the same time?

Always consult the documentation. Apple says, in a big box with an exclamation mark graphic next to it, in the section on frame:

Warning: If the transform property is not the identity transform, the
value of this property is undefined and therefore should be ignored.

Underneath centre it says:

The center is specified within the coordinate system of its superview
and is measured in points. Setting this property changes the values of
the frame properties accordingly.

So the answer you link to is incorrect. Given its one-sentence nature with no reference to sources, probably the author tried it once, on the version of iOS he happened to have installed, it looked like it worked and he jumped straight to an unsupported conclusion.

The other answer is correct. You need to build the translation into your transform. Likely by throwing in a use of CGAffineTransformTranslate.

EDIT: so, to set a transform with a translation of (1000, 0) and a rotation of -M_PI/2:

_viewToDrag.transform = CGAffineTransformRotate(
CGAffineTransformMakeTranslation(1000.0, 0.0),
-M_PI_2);

Rotate UIImageView with same center point using CGAffineTransformMakeRotation

Turns out the weird rotation issue was related to the new iOS 6 autolayout feature. Once I took some time to understand how the align constraints worked I was able to achieve the results I wanted.

If you run into weird rotation issues and are using iOS 6 with autolayout enabled chances are your constraints need adjusting.

The key is to vertically and horizontally center the image under rotation in it's parent view.

Rotate an UIView with CGAffineTransformMakeRotation

When you add the transform to myView you are not applying a transform to it's existing transformed state, but rather overwriting the transform. This means that when you set transform = CGAffineTransformMakeRotation(M_PI/2) for the second time, you are not actually changing the transform at all.

EDIT:
As Rob pointed out, there is no need to use the property rotationInRadians when the rotation value is already stored in the transform. Instead you can simply modify the existing transform like this:

[UIView animateWithDuration:0.2 animations:^{
_myView.transform = CGAffineTransformRotate(_myView.transform, M_PI/2);
}];

Below is the not ideal solution that I previously presented.

You will need a variable that keeps track of your rotation. You will increment this variable by your desired amount each time you want to rotate, then create a new transform based on the new value of the variable.

Make a property to do this:

@property (nonatomic, assign) CGFloat rotationInRadians;

Initialize it to 0 in your init method. Then modify your rotateView method to increment rotationInRadians and apply the new transform.

- (IBAction)rotateView:(id)sender {
self.rotationInRadians += M_PI/2;
[UIView animateWithDuration:0.2 animations:^{
_myView.transform = CGAffineTransformMakeRotation(self.rotationInRadians);
}];
}


Related Topics



Leave a reply



Submit