Image rotates only once - iphone

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.

Related

ios growth animation CGAffineTransformMakeScale moves around

I am animating a tree that grows. Like a Tree, I want my UIImage to stay in the same place, so that when I scale it up it appears to grow.
Right now its jumping around, i.e. its growing from the centre - I need it to grow from the bottom.
Is there a way to set the origin/base of the animation. So the scale animation works from the bottom.
Hope that makes sense. here is my code:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
tree.transform = CGAffineTransformMakeScale(1.0f, 1.25f);
[UIView commitAnimations];
You may wish to look at the CALayer class for this and set the anchorPoint to the centerbottom
- (void)viewDidLayoutSubviews
{
self.outletTestView.layer.anchorPoint = CGPointMake(0.5, 1.0);
self.outletTestView.layer.position = CGPointMake(100, 300);
}
Don't forget to include:
#import <QuartzCore/QuartzCore.h>
And use for the scaling:
CGAffineTransform CGAffineTransformScale ( CGAffineTransform t, CGFloat sx, CGFloat sy );
Like so:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
tree.transform = CGAffineTransformScale(tree.transform, 1.0f, 1.25f);
[UIView commitAnimations];

How do I discard Image Position after Animation on iOS

I am doing a scale and translate animation using the code below. This moves an image from bottom left to center of screen. Next time when I do animation I want to restart from bottom left. But the animation doesn't start. What do I need to restart the animation ?
// 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);
CGAffineTransform scale = CGAffineTransformMakeScale(2.0,2.0);
CGAffineTransform scale_transform = CGAffineTransformConcat(transform,scale);
image.transform = scale_transform;
[UIView commitAnimations];
I found out. This is what solved it.
image.transform = CGAffineTransformIdentity

iPhone - How to rotate compass for North magneticHeading

I have a compass/image where the needle should always point North.
I have the compass needle/dial rotating using the following conditional statement:
double heading = newHeading.magneticHeading;
if( heading >= 0.0 && heading <= 23.0) {
needleView.transform=CGAffineTransformMakeRotation(heading);
NSLog(#"N");
currentDirLabel.text = #"N";
}
I was wondering if anyone knew how I'd go about rotating the needleView to always point North.
Here's the code I'm using to rotate the needleView UIView:
- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve degrees:(CGFloat)degrees
{
// Setup the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
CGAffineTransform transform =
CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
image.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
To clarify: the UIView (needleView) is spinning as the magneticHeading changes. I'm looking to always lock the the image in the Northerly direction (in this case the part of the image with the North needle)
thanks for any help.
this book: Oreilly.Basic.Sensors.in.iOS.Jul.2011 helped me solved this.

iphone image move like another image

I have an image which is rotating and what I want is that a second image to be stuck on it and it rotate just as the first. Please, how can I do this?
I'm not at my computer so I can't test this, but I think it should get you what you want.
// assuming you already have your views
// viewOne, viewTwo
[viewOne addSubview:viewTwo]; // add viewTwo to viewOne
// let's assume that you call this code here one time per second
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
// when this is 1.0, the image will rotate 360
// ... when it is .5, it should rotate 180, etc...
float rotationFactor = 0.1f;
// viewTwo's rotationFactor will be added to view 1's in the transform below
float rotationFactor2 = 0.1f;
viewOne.transform = CGAffineTransformMakeRotation(M_PI_2 * rotationFactor);
viewTwo.transform = CGAffineTransformMakeRotation(M_PI_2 * (rotationFactor + rotationFactor2);
[UIView commitAnimations];

Animated moving and rotating a UIView doesn't quite work

I want to create an animation that moves and rotates a UIView at the same time. I tried the following code:
[UIView beginAnimations:#"MoveAndRotateAnimation" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:kAnimationDuration];
myView.frame = newFrame;
myView.transform = CGAffineTransformMakeRotation(0.4);
[UIView commitAnimations];
The result is, that after the animation has finished the view is drawn incorrectly (some parts are not visible anymore). If I only change the frame OR the transformation for the animation, the view draws correctly. The problem only occurs if I set both the frame and transformation.
What would be the correct way to animate moving and rotating of a view at the same time?
You can't use frame, bounds, or center if you are also animating things like rotation. You need to use CGAffineTransforms for everything and concatenate them together. Like so:
CGAffineTransform transform = CGAffineTransformMakeTranslation(100,100);
transform = CGAffineTransformRotate(transform, 0.4);
[UIView beginAnimations:#"MoveAndRotateAnimation" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:kAnimationDuration];
myView.transform = transform;
[UIView commitAnimations];
Try doing it this way: (Moving left and rotating, just for example)
[UIView beginAnimations:#"MoveAndRotateAnimation" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:kAnimationDuration];
CGPoint center = CGPointMake(-200, self.view.center.y);
self.view.transform = CGAffineTransformMakeRotation(-0.5);
self.view.center = center;
[UIView commitAnimations];
The center point locations would be the point you wish to make the view move, of course.
The problem is probably with your new frame size as compared to original frame size. View frame is a bit tricky when you rotate a frame:
Frame is in superviews coordinate system. So, when you rotate a rectangle, the new frame will be the smallest rectangle in superview's coordinate system which contains the view.
e.g. if you have a square view with frame size of (100*100) and rotate it by 45 degrees your new frame would have a size of (141*141) and if you set the frame to have a size of (100*100) you'll cut part of your view.
Look at here for better understanding of how view frame works.
You need to do this:
#pragma mark (Woblling animation)
CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-10.0));
CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(10.0));
self.view .transform = leftWobble; // starting point
[UIView beginAnimations:#"wobble" context:self.view ];
[UIView setAnimationRepeatAutoreverses:YES]; // important
[UIView setAnimationRepeatCount:11];
[UIView setAnimationDuration:1.00];
//[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(wobbleEnded:finished:context:)];//should be same as written
self.view .transform = rightWobble; // end here & auto-reverse
[UIView commitAnimations];
and then implement these methods. They are necessary to keep your view in the same position after rotation.
- (void) wobbleEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if ([finished boolValue]) {
UIView* item = (UIView *)context;
item.transform = CGAffineTransformIdentity;
}
}
Use this method if you are using navigation and when you navigate from one view to another, then your view would be at the same place.else leave this method:
- (void)viewWillAppear:(BOOL)animated
{
UIView* item = self.view;
item.transform = CGAffineTransformIdentity;
}
I had the same problem but did some experiments and came up with this solution.