Obj-C: It changes center before rotating - objective-c

It's my code:
[UIView animateWithDuration:0.4f
delay:0.1f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
CGAffineTransform rotate = CGAffineTransformMakeRotation( 30.0 / 180.0 * 3.14 );
[needleBig setTransform:rotate];
}
completion:^(BOOL finished){
}];
And it's the pictures before and after rotating:
It seems it changes center before rotating, but I don't want it.

A rotation created with CGAffineTransformMakeRotation(angle) makes a rotation of the coordinate system around its origin. The docs say "angle = The angle, in radians, by which this matrix rotates the coordinate system axes."
From your pictures it looks as if the origin of your coordinate system is somewhere at the top left corner of the square enclosing your clock.
You can rotate about any other point if you set the view's layer's anchorPoint property accordingly.

This worked:
needleBig.layer.anchorPoint=CGPointMake(0.5f, 0.5f);
[UIView animateWithDuration:0.4f
delay:0.01f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
CGAffineTransform rotate = CGAffineTransformMakeRotation(180.0f * (M_PI / 180.0f));
[needleBig setTransform:rotate];
}
completion:^(BOOL finished){
}];

Related

UIView animation to move and back

I would like to have animation on a view, that it will go 2 pixels right on x, and 2 pixels back left on x .
I have this for opacity, and i am looking to adjust this to make it ,
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:#"opacity"];
theAnimation.duration=1.0;
theAnimation.repeatCount=HUGE_VALF;
theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
theAnimation.toValue=[NSNumber numberWithFloat:0.3];
[image.layer addAnimation:theAnimation forKey:#"animateOpacity"];
If you just want the animation to occur once then you can jump up a few levels of abstraction and use the block based view animations
[UIView animateWithDuration:1.f
delay:0
options:UIViewAnimationOptionAutoreverse
animations:^{
view.transform = CGAffineTransformMakeTranslation(2.f, 0.f);
} completion:^(BOOL finished) {
view.transform = CGAffineTransformIdentity;
}];
This applies a translation of two points right and then removes it on completion

How do i shrink an image horizontally using CGAffineTransform?

I've been using CABasicAnimation to move around images. Now I need to take a view and shrink it horizontally. On the web I see some people using CGAffineTransform to shrink/grow images proportionally. I need the view to shrink horizontally ONLY. See image below:
Any help with this would be greatly appreciated!
CGAffineTransformMakeScale(sx,sy) will do the work.
CGAffineTransform a = CGAffineTransformMakeScale(0.4, 1.0);
editingView.transform = a;
You can use [UIView animateWithDuration:animations:completion:]; if you want it to be animated.
CGAffineTransform a = CGAffineTransformMakeScale(0.4, 1.0);
[UIView animateWithDuration:0.5
animations:^{
editingView.transform = a;
}
completion:^(BOOL finished) {
/* do something after the animation */
}];

Animate Rotation/Scale/Translate of UIImage at the same time

I am new to objective-c and I want to add an image to the screen, tweening it like in AS3, moving it from one end to the other of the screen while rotating around its own center point.
I tried with
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
// TRANSFORM SCREENSHOT
screenShotView.transform = CGAffineTransformRotate(screenShotView.transform, -M_PI * 0.05);
screenShotView.transform = CGAffineTransformScale(screenShotView.transform, 0.6, 0.6);
screenShotView.transform = CGAffineTransformTranslate(screenShotView.transform,
self.webView.frame.origin.x,
self.webView.frame.origin.y - self.webView.frame.size.height * 0.3
);
but with this code the image rotates around the center of the TransformIdentity. So while rotating and moving the rotation gets out of controll and the image isn't exactly at the position I loved it to be.
What is the right way to rotate and translate at the same time, translating the rotation center with the image?
and at least after transformation I want to add a close button to the right upper corner of the image. for that I need the new coordinates of the corner, too.
thnx!
I now ended with the following code, but I still don't know if this is the state of the art solution.
CGAffineTransform translate = CGAffineTransformMakeTranslation(self.webView.frame.origin.x,self.webView.frame.origin.y - self.webView.frame.size.height * 0.25);
CGAffineTransform scale = CGAffineTransformMakeScale(0.6, 0.6);
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformRotate(transform, degreesToRadians(-10));
[UIView beginAnimations:#"MoveAndRotateAnimation" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:2.0];
screenShotView.transform = transform;
[UIView commitAnimations];
IOS 7's preferred way to do this would be using a block object. It has several advantages compared to the 'older' way of animating. Especially that it can make use of multi-core and video co-processing. Also the 'built-in' call back part (the completion part) is very useful, as it keeps any necessary state information and links to objects as needed.
CGAffineTransform translate = CGAffineTransformMakeTranslation(self.webView.frame.origin.x,self.webView.frame.origin.y - self.webView.frame.size.height * 0.25);
CGAffineTransform scale = CGAffineTransformMakeScale(0.6, 0.6);
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformRotate(transform, degreesToRadians(-10));
// animation using block code
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
screenShotView.transform = transform;
}completion:^(BOOL finished){
// do something if needed
}];

Making angle rotations to a UIImageView but it doesn't make whats expected

I'm trying to make a turret that turns to the location thats touched and then shoots the bullet
So far there is no problem but I couldn't figure out why the turret does not face the direction I touched
NOTE: I forgot to tell that it turns but not to the specific location that I touched.
I looked to this sources but they didn't helped
http://www.cocos2d-iphone.org/forum/topic/1823
http://stackoverflow.com/questions/3018688/rotating-and-moving-a-uiimageview-cocoatouch
here is my code that does the rotation. What am I missing?
-(void)update:(UITapGestureRecognizer *)recognizer
{
double dx,dy;
CGPoint location = [recognizer locationInView:self.view];
turret.transform=CGAffineTransformIdentity;
bullet.transform=CGAffineTransformIdentity;
bullet.center=turret.center;
if ( location.x < 160 )
{
dx=turret.center.x-location.x;
dy=turret.center.y-location.y;
//if user touched greater than 180
}
else if ( location.x > 160 )
{
dx=location.x-turret.center.x;
dy=location.y-turret.center.y;
//if user touched less than 180
}
CGAffineTransform transform = CGAffineTransformMakeRotation((atan2(dy,dx)*180/M_PI));
NSLog(#"%f",atan2(dy,dx)*180/M_PI);
bullet.transform=transform;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
turret.transform = transform;
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];
bullet.center=location;
[UIView commitAnimations];
I believe CGAffineTransformMakeRotation uses radians not degrees, so don't multiply the output of atan2(dy, dx) by 180/pi (although you can when you NSLog it if you want it logged in degrees).

Obj C: How to scale only to up?

Hey everbody. I have a little issue here. I just want to scale my view, but only to up. I mean, i dont want it move out from original coords, i just want to scale in one direction, without affecting the position.
[UIView beginAnimations: #"myViewAnimation" context: nil];
[UIView setAnimationDuration:1.0];
redView.transform = CGAffineTransformMakeScale(1.0, 3.0);
[UIView commitAnimations];
Thanks!
Origin of all UIView transforms is its layer's anchorPoint, which is in the center of the view by default. Set anchorPoint to CGPointZero so view will be scaled to the right and upwards and its current position will be fixed:
redView.layer.anchorPoint = CGPointZero;
...
UIView beginAnimations: #"myViewAnimation" context: nil];
[UIView setAnimationDuration:1.0];
redView.transform = CGAffineTransformMakeScale(1.0, 3.0);
[UIView commitAnimations];
Note also that view's frame is calculated using its layer's anchorPoint so you may need to also adjust your view's frame accordingly
To access view's layer you will need to add QuartzCore.framework to link with your project and import <QuartzCore/QuartzCore.h> header file.