How do I rotate a subview x degrees? - iphone

So I want to rotate a subview x degrees (or at least 90 degrees). I want the subwiew to rotate around the subview's bottom right corner. Is it possible to do this animated like in the picture below?
Thanks!

CGAffineTransform transform =
CGAffineTransformMakeRotation(angle);
yourSubView.transform = transform;

Yo can do it composing transforms. First translate to the rotation origin, then rotate, then transform back to the original center. The view you are rotating is v. It is inside an animation just for fun.
[UIView beginAnimations:nil context:NULL];
CGAffineTransform t = CGAffineTransformIdentity;
t =CGAffineTransformTranslate(t,- v.bounds.size.width/2, -v.bounds.size.height/2);
t = CGAffineTransformRotate(t, -M_PI/2);
t =CGAffineTransformTranslate(t,v.bounds.size.width/2, v.bounds.size.height/2);
v.transform = t;
[UIView commitAnimations];

Related

Image rotates only once

I walked over other same question but it didn't help.
I have an image that should rotate every doubleTap by 90 degrees
This is the rotation function
-(void) rotateImageWithAngle:(float)angle1
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
[UIView setAnimationBeginsFromCurrentState:YES];
CGAffineTransform transform;
transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(angle1));
self.transform = transform;
[UIView commitAnimations];
}
angel1 is always 90.
The problem is that it rotates only once, every additional doubleTap goes to the function but does nothing.
Thanks
You're applying the same transform every time. You should change the angle by 90 degrees with each double tap.

iOS - Animation CGAffineTransformTranslate - Hides under Navigation Bar

I'm doing an animation where I'm moving and scaling an image at the same time.
I want to move the image onto a toolbar button in the top navigation bar. It scales down to zero as it does this so it looks like the image is being saved to that location....similar to what happens when you download a song in iTunes on the iPhone.
My scaling and the translation are working fine, however the image disappears under the navigation bar. I want it to translate on top of the navigation bar and then disappear.
Does anyone know how I can tell my animation not to go under the navigation bar?
My code is below.
Thanks
Brian
CGAffineTransform translate = CGAffineTransformMakeTranslation(160, -160);
CGAffineTransform scale = CGAffineTransformMakeScale(0.1, 0.1);
CGAffineTransform transform = CGAffineTransformConcat(scale, translate);
//transform = CGAffineTransformRotate(transform, 20);
[UIView beginAnimations:#"MoveAndScaleAnimation" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
imageView.transform = transform;
[UIView commitAnimations];

Landscape mode UIView.center gives Portrait mode coordinates

I have an app, it runs in landscape mode, I hold it in landscape mode when debugging on my phone. I pick up the center value from the UIView on my main view controller, I need this to put items in the center of the screen, and since it is universal I need this to be variable and not set to iPhone screen sizes.
When I do this and read the x and y from the CGPoint returned by view.center I get
x = 170
y = 240
The delegate is the one viewcontroller in my app, the center function is one of the object I want to move to the center.
- (void) center
{
CGPoint center = ((UIViewController*)delegate).view.center;
CGSize size = self.frame.size;
double x = center.x - size.width/2 - location.x;
double y = center.y - size.height/2 - location.y;
[self _move:1.0 curve:UIViewAnimationCurveEaseInOut x:x y:y];
}
- (void)_move: (NSTimeInterval)duration
curve:(int)curve x:(CGFloat)x y:(CGFloat)y
{
// Setup the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
self.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
Now to me this does not make much sense.
Use bounds, not frame. Frame is in the superview's coordinate system which for your root view is the window - this does not rotate in different orientations.
Bounds is in the views own coordinate system so is the appropriate rect to use when setting center on subviews, since center is also expressed in the superview's coordinate system.

How To Create A Center Point At UIImageview

Hi Guys I am trying to rotate a UIImageview from the Black dot on this image.
what is happening now is that the center is not changing
I have tried like this.
Image.Center = CGPointmake(37.3,150);
when I am calling this set the image just moves across the screen.
thanks!
You need to add this code:-
imageView.layer.anchorPoint = CGPointMake(0.5,0.5);
[UIView beginAnimations:#"rotate" context:nil];
imageView.transform = CGAffineTransformMakeRotation(angle);
[UIView commitAnimations];

zooming from a particular point

I am using this code to zoom from a particular point
CGPoint getCenterPointForRect(CGRect inRect)
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
return CGPointMake((screenRect.size.height-inRect.origin.x)/2,(screenRect.size.width-inRect.origin.y)/2);
}
-(void) startAnimation
{
CGPoint centerPoint = getCenterPointForRect(self.view.frame);
self.view.transform = CGAffineTransformMakeTranslation(centerPoint.x, centerPoint.y);
self.view.transform = CGAffineTransformScale( self.view.transform , 0.001, 0.001);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration];
self.view.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
Its not working. What is the correct way to do zooming from a particular point.
I think that, if I've diagnosed your problem correctly, you're getting a scaling animation where the view starts tiny and at a point, then scales and moves to the centre of the screen exactly like you want, but the point it starts at is incorrect?
First of all, views scale around their centre. So if you took out the translation and hence reduced the code you have to:
self.view.transform = CGAffineTransformMakeScale( 0.001, 0.001);
And your view ends up taking up the whole screen then it'll remain centred on the middle of the screen, sort of a little as though it's a long way away and you're heading directly towards it.
Supposing you instead want it to grow and move to the centre of the screen from (x, y) then you need something more like:
CGPoint locationToZoomFrom = ... populated by you somehow ...;
CGPoint vectorFromCentreToPoint = CGPointMake(
locationToZoomFrom.x - self.view.center.x,
locationToZoomFrom.y - self.view.center.y);
self.view.transform = CGAffineTransformMakeTranslation(vectorFromCentreToPoint.x, vectorFromCentreToPoint.y);
self.view.transform = CGAffineTransformScale( self.view.transform , 0.001, 0.001);
Where locationToZoomFrom will be the initial centre of the view and its normal centre as per its frame will be the destination.