how can I interrupt/stop animation when I rotate? - ios

For example, I use the animation in landscape status, duration is 5.0s, from status A to B; in the middle of the 5.0s, I may rotate the iPad from landscape to portrait. I want the animation stopped and make the UI status to C after I rotated.
I'm not sure my question is clear.
How can I do that?
my animation code:
- (void)moveImage:(UIImageView *)image duration:(NSTimeInterval)duration x:(NSNumber*)dx y:(NSNumber*)dy
{
// Setup the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
float fx = [dx floatValue];
float fy = [dy floatValue];
CGAffineTransform transform = CGAffineTransformMakeTranslation(fx, fy);
//CGAffineTransform transform = CGAffineTransformMakeRotation(0.4);
//CGAffineTransform transform = CGAffineTransformMakeScale(2.0, 2.0);
image.transform = transform;
// Commit the changes
[UIView commitAnimations];
}

Starting and Stopping Explicit Animations has a section on starting and stopping core animations
willRotateToInterfaceOrientation:duration notifies you when the rotation begins.
So all have to do is 1) from 2)

Related

Animation shows unwanted shadow

I'm animating an image to move up 50px, but as it moves, it leaves a shadow. How do I remove this shadow? I'd like the image to move smoothly. The shadow is only really present at the beginning of the animation.
Method used:
- (void)moveImage:(UIImageView *)image duration:(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);
image.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
Calling the method:
[self moveImage:imageToMove duration:1.0
curve:UIViewAnimationCurveLinear x:0.0 y:-70.0];
Image:

Keep back to origin position an UIImageView

I'm now implementing an app, for iOS devices so in objective C, and i was asking how to keep back to origin position an UIImageView after a rotation ?
For example, i use my UIImageView like this :
CGAffineTransform initialPosition = CGAffineTransformMakeRotation(0);
CGAffineTransform finalPosition = CGAffineTransformMakeRotation(M_PI/2);
[UIView beginAnimations: #"Rotate animation" context: NULL];
MySuperUIImageView.transform = initialPosition;
[UIView setAnimationDuration:1.0];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationDelegate:self];
MySuperUIImageView.transform = finalPosition;
[UIView commitAnimations];
But after, i made it desappear for a time, using :
MySuperUIImageView.alpha = 0.0f;
And when i re-use it, i want it to be in the first original position, with no rotation. Is it possible easily ?
Thanks in advance !
Yes. Very easily:
MySuperUIImageView.transform = CGAffineTransformIdentity;
Actually, there is. Since you're applying an affine transform to the view, you can reset all transforms applied to the view by setting its transform to CGAffineTransformIdentity which applies an identity transform matrix:
1 - 0 - 0
0 - 1 - 0
0 - 0 - 1
[mySuperUIImageView setTransform:CGAffineTransformIdentity];
Note: In Objective C, the convention is to start class names with a capital letter, and instances with a lower case.
As of iOS 4, Apple recommends using block based UIView animation. Here's an example of what I believe you're trying to attempt using updated code.
CGAffineTransform finalPosition = CGAffineTransformMakeRotation(M_PI/2);
[UIView animateWithDuration:1.0 delay:0.0 options:kNilOptions animations:^{
[mySuperUIImageView setTransform:finalPosition];
} completion:^(BOOL finished) {
[mySuperUIImageView setTransform:CGAffineTransformIdentity];
}];

Image is jumping before animating

I currently have an image animating on the click of a button, but the problem is that the image is jumping from it's position on the storyboard before it starts the animation. I cannot figure out why it's doing this - all I would like to do it move it from its current position off the screen to the right.
Am I doing something wrong, or just missing something?
Original position:
Beginning of animation:
moveImage trigger:
[self moveImage:_cornerCloud duration:3.0
curve:UIViewAnimationOptionCurveLinear x:200.0 y:0];
moveImage function:
- (void)moveImage:(UIImageView *)image duration:(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);
image.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
[UIView animateWithDuration:3.0f
delay:0.0f
options:UIViewAnimationOptionCurveLinear
animations:^{
// just set the center to off-screen
}
completion:^(BOOL finished){}];
That should be a bit easier.

CALayer rotate by direction

I'm trying to rotate a CALayer, first of all I rotate it by -30 degrees, so it turns left.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
self.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-30));
[UIView commitAnimations];
Now I want it to complete the rotate, do another rotation starting left, but it takes the shortest path.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
self.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-30));
[UIView commitAnimations];
How can I force it's rotation direction ?
You can use a CABasicAnimation to rotate to a specific angle.
CABasicAnimation *rotate =
[CABasicAnimation animationWithKeyPath:#"transform.rotation"];
rotate.toValue = #(M_PI*2);
rotate.duration = 1.0; // your duration
[yourView.layer addAnimation:rotate
forKey:#"myRotationAnimation"];
There are some more parameters that you can use to configure how the animation looks (for example timing function).
By default the animation will remove itself after it completes so you should update to the end value (this has been explained over and over in all the "how do I rotate ..."-questions.
Also, you need QuartzCore.framework for Core Animation (CALayer and CABasicAnimation in this example)

Trying to animate one UIImageView with two animations, only one animation has duration

What I want to do is flip this UIImageView in an instant, no animation, then I need it to float to it's intended position on the screen. Both of the transformations are working, but not the way I want them to.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
resultsEnemy.transform = CGAffineTransformMakeTranslation(0, 0);
[UIView commitAnimations];
resultsEnemy.transform = CGAffineTransformMakeScale(-1, 1);
This is the code I am using. Despite the fact that the scale code (which I am using to flip the UIImageView) is not part of the animation with the 0.5 duration, it is following those rules. How do I avoid this?
Applying two transforms like that isn't going to produce the result you expect. What you need to do is combine them into a single transform matrix. The following should work as expected.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
// Create two separate transforms and concatenate them together.
// Use that new transform matrix to accomplish both transforms at once.
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 0);
CGAffineTransform scale = CGAffineTransformMakeScale(-1, 1);
resultsEnemy.transform = CGAffineTransformConcat(translate, scale);
[UIView commitAnimations];
Edit: Based off of your clarification, you seem to be wanting something like this:
CGAffineTransform scale = CGAffineTransformMakeScale(-1, 1);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 0);
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
resultsEnemy.transform = scale;
[CATransaction commit];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
resultsEnemy.transform = CGAffineTransformConcat(translate, scale);
[UIView commitAnimations];

Resources