I have a speedometer widget I have created in my app using custom animation and I am having problems getting it to animate the way I want.
The pick animates to a particular point depending on a value that has been passed in. It ranges from 0 – 50. So if its at 50, the pick will rotate around all the way to the right, it will remain to the left hand side. I want however, at the start for the pick to always animate all the way to the right, then back down to its correct position. This is the part I am having problems with.
For time being I have hard coded all the values just for testing purposes.
The pick is in the centre at the start, so I need make it appear rotated to the left.
image.transform = CGAffineTransformRotate(image.transform, -0.8);
Then I need to animate it to the far right so I use this
[UIView animateWithDuration:2.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
image.transform = CGAffineTransformRotate(image.transform, 1.7);
}
completion:nil];
This works fine, so when the app starts, the pick will start from the left, then rotate all the way to the right.
The problem comes when I add this final part in
I then need it to rotate from the right back to the point where it needs to be. For time being I have set this to the centre.
[UIView animateWithDuration:2.0 delay:2.0 options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
image.transform = CGAffineTransformRotate(image.transform, -0.8);
}
completion:nil];
When the app starts, the pick starts off at the right hand side, then rotates to the centre. It is ignoring the first part where it should be starting off rotated to the left.
Does anyone know why this happens? Been at this for ages now and can’t seem to figure it out.
Edit: I tried sticking the final animation inside this code here
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
});
The pick will animate from left to right, but then it just jumps from the right to the centre point, rather than animating to the centre point :@
Tried this:
-(void) animateToEnd : (UIImageView *)image
{
[UIView animateWithDuration:2.0 delay:0 options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
image.transform = CGAffineTransformRotate(image.transform, 1.7);
image.transform = CGAffineTransformIdentity;
}
completion:nil];
}
-(void) animateToFinalPosition : (UIImageView *)image
{
[UIView animateWithDuration:2.0 delay:2.0 options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
image.transform = CGAffineTransformRotate(image.transform, -0.8);
image.transform = CGAffineTransformIdentity;
}
completion:nil];
}
-(void) speedometer : (UIImageView *)image
{
image.transform = CGAffineTransformRotate(image_p.transform, -0.8);
[self animateToEnd:image];
[self animateToFinalPosition:image];
}
It is ignoring the left to right animation, and just starts from the very left
Fixed problem. Changed the animation to this
-(void) animateToEnd : (UIImageView *)image_p
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
image_p.transform = CGAffineTransformRotate(image_p.transform, 1.7);
[UIView commitAnimations];
}
-(void) animateToPosition : (UIImageView *)image_p : (float)temp
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
NSLog(@"Dispatch");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelay:0.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
image_p.transform = CGAffineTransformRotate(image_p.transform, -temp);
[UIView commitAnimations];
});
}
That block code I was using before seem to be causing the issue
Do you reset your identitymatrix before applying another rotation:
If it´s not reset then each rotation is applied on top of the previous one. This may be a part of your problem.