Use CGAAffineTransform to rotate a + symbol in the middle? - iphone

I want to rotate a + to become an x, but it's not rotating via the center of the +. How can i get it to rotate via that point?

As you suggest just rotate the label :)
CGAffineTransform rotare = CGAffineTransformMakeRotation(45*M_PI/180.0f);
[plusLabel setTransform:rotare];
If the label is not rotating around the + center include the QuartzCore and rotate the [plusLabel layer]
CATransform3D rotate3D = CATransform3DMakeRotation(45*M_PI/180.0f, 0, 0, 1);
[[plusLabel layer] setAnchorPoint:CGPointMake(0.4, 0.6)]; // The center of Plus in my case
[[plusLabel layer] setTransform:rotate3D];

Related

Rotating UIImageView Moves the Image Off Screen

I have a simple rotation gesture implemented in my code, but the problem is when I rotate the image it goes off the screen/out of the view always to the right.
The image view that is being rotated center X gets off or increases (hence it going right off the screen out of the view).
I would like it to rotate around the current center, but it's changing for some reason. Any ideas what is causing this?
Code Below:
- (void)viewDidLoad
{
[super viewDidLoad];
CALayer *l = [self.viewCase layer];
[l setMasksToBounds:YES];
[l setCornerRadius:30.0];
self.imgUserPhoto.userInteractionEnabled = YES;
[self.imgUserPhoto setClipsToBounds:NO];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotationDetected:)];
[self.view addGestureRecognizer:rotationRecognizer];
rotationRecognizer.delegate = self;
}
- (void)rotationDetected:(UIRotationGestureRecognizer *)rotationRecognizer
{
CGFloat angle = rotationRecognizer.rotation;
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, angle);
rotationRecognizer.rotation = 0.0;
}
You want to rotate the image around it's center, but that's not what it is actually happening. Rotation transforms take place around the origin. So what you have to do is to apply a translate transform first to map the origin to the center of the image, and then apply the rotation transform, like so:
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, self.imageView.bounds.size.width/2, self.imageView.bounds.size.height/2);
Please note that after rotating you'll probably have to undo the translate transform in order to correctly draw the image.
Hope this helps
Edit:
To quickly answer your question, what you have to do to undo the Translate Transform is to subtract the same difference you add to it in the first place, for example:
// The next line will add a translate transform
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 10, 10);
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, radians);
// The next line will undo the translate transform
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, -10, -10);
However, after creating this quick project I realized that when you apply a rotation transform using UIKit (like the way you're apparently doing it) the rotation actually takes place around the center. It is only when using CoreGraphics that the rotation happens around the origin. So now I'm not sure why your image goes off the screen. Anyway, take a look at the project and see if any code there helps you.
Let me know if you have any more questions.
The 'Firefox' image is drawn using UIKit. The blue rect is drawn using CoreGraphics
You aren't rotating the image around its centre. You'll need correct this manually by translating it back to the correct position

Rotate CGRect property of a CCSprite ?

I have a sublclass of a CCSprite that rotates throughout the game and there is a shield that has to rotate around the sprite according to the sprite's rotation. So if the sprite's rotation is 75 degrees there should be a CGRect located at 75 degrees. The dimensions of the CGRect are subordinate as it almost resembles a square.
What I did is:
I subclassed CCSprite and added a property called shieldArea.
Upon initialization I set this rect to be
self.shieldArea = CGRectMake(self.position.x-30, self.position.y, 8, 10);
Then I rotate the sprite itself, however, the rect stays at its initial position.
I hoped that the CGrect would be affected by the rotation, but I kind of expected it not to affect it, of course, why should it ? So, my question is, how do I rotate a CGRect at all ? Or do I have to add a new CGRect all the time ?
Side notes: I do not want to use Box2d or anything the like. I handle collision detection myself.
Have you tried CGAffineTransform?
Something like this:
float centerX = myOldRect.origin.x + (myOldRect.size.width / 2.0);
float centerY = myOldRect.origin.y + (myOldRect.size.height / 2.0);
CGAffineTransform rotation = CGAffineTransformMakeRotation(someAngleInRadians);
CGAffineTransform moveAnchor = CGAffineTransformMakeTranslation(centerX, centerY);
CGAffineTransform centeredRotation = CGAffineTransformConcat(moveAnchor, rotation);
CGRect rotatedRect = CGRectApplyAffineTransform(myOldRect, centeredRotation);
Note, this is NOT tested. Use at your own risk :p

UIimageView rotate around a center point of another UIImageView

I have two images.
One image is rectangle
second is triangle
i want to rotate tringle image from the center point of rectangle image using one finger or touch event.
Well, the math for this will get decently complex if you wish for the image to rotate in a circle of any size around the rectangles center point, but a good start would be to define the anchor point of the triangle image view, e.x.
#import <QuartzCore/QuartzCore.h>
#define degreesToRadians(x) (M_PI * x / 180.0)
[[triangleImageView layer] setAnchorPoint:CGPointMake:(0.5,0.0)];
[UIView animateWithDuration:1.0 animations:^{
[triangleImageView setTransform:CGAffineTransformMakeRotation(degreesToRadians(90))];
}];
Finally i found that i have to use [view.layer setAnchorPoint:CGPointMake(0.5, 0.5)]
and it solve my problem.

Spinning Wheel Game: Trouble with rotation image [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can I use CGAffineTransformMakeRotation to rotate a view more than 360 degrees?
I'm trying to create a game where pushing a button spins an dial image, having trouble with the rotation code. Goal is to spin the image from its current position by a % of 360, i.e .4 * 360, spin the image 144 degrees. Thanks Guys!
-(IBAction)spin:(id)sender
{
float strenght = DEGREES_TO_RADIANS(.5 * 360);
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationCurveEaseOut animations:^
{
[spinner_iv setTransform:CGAffineTransformRotate([spinner_iv transform],strenght)];
}
completion:^(BOOL finished)
{
//done
}];
}
UIView animations using CGAffineTransform take the 'shortest path', so if you request a clockwise rotation of 270 degrees, it will animate anticlockwise by 90 degrees.
You can get any number of spins if you instead rotate the view layer using CATransform3D - yes, use this even if you are rotating in 2D. See Can I use CGAffineTransformMakeRotation to rotate a view more than 360 degrees?
Converting degrees to radians is
Radians = Degrees * (PI/180)

Co-ordinates of the four points of a uiview which has been rotated

Is it possible to get this? If so, can anyone please tell me how?
Get the four points of the frame of your view (view.frame)
Retrieve the CGAffineTransform applied to your view (view.transform)
Then apply this same affine transform to the four points using CGPointApplyAffineTransform (and sibling methods of the CGAffineTransform Reference)
CGPoint topLeft = view.bounds.origin;
topLeft = [[view superview] convertPoint:topLeft fromView:view];
CGPoint topRight = CGPointMake(view.bounds.origin.x + view.bounds.width, view.bounds.origin.y);
topRight = [[view superview] convertPoint:topRight fromView:view];
// ... likewise for the other points
The first point is in the view's coordinate space, which is always "upright". Then the next statement finds the point that point corresponds to in the parent view's coordinate space. Note for an un-transformed view, that would be equal to view.frame.origin. The above calculations give the equivalent of the corners of view.frame for a transformed view.