So I want to do a UI animation where a view gradually fades out. Right now I have something like this
[UIView animateWithDuration:2.0f
animations:^{
myView.alpha = 0.0f;
}
];
So right now over the course of those 2 seconds, the view will linearly gradually fade out. However I want to make it so that the fade out is non-linear. For instance, I want it to start fading out slowly and then fade more quickly over time.
How would I do this?
Use one of the longer forms of animateWithDuration methods that take an options parameter, like animateWithDuration:delay:options:animations:completion:.
One of the options you can supply is a timing curve like UIViewAnimationOptionCurveEaseInOut.
Your choices are
UIViewAnimationOptionCurveEaseInOut = 0 << 16,
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,
If you use CAAnimation instead of UIView animation, you can also create your own custom timing function using the control points of a bezier curve that describes the desired timing function. However, CAAnimation is more complex to use and less well documented.
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
myView.alpha = 0.0f;
} completion:nil];
Or pick your favorite time curve instead of UIViewAnimationOptionCurveEaseInOut
You must be needing about "ease in" timing curve in which the animation starts slowly and ends as linear. Try the below code,
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
myView.alpha = 0.0f;
} completion:nil];
Please do let me know, if it solves your problem.
Related
I'm trying to rearrange the content of my UIView based on a user event (rotation), and I've found that I would like to combine a translation animation, and the fading out and fading in of various controls.
Since this seems to depend on the "options" of the animate functions, I assumed that I need to launch two animations concurrently with different options, as follows. The "Layout_Set" function assigns new "frames" to all of my controls, which works fine. The "setHidden" in the block of the other animation is intended to fade out a single control on rotation.
However... doesn't work. Is this the right way to (a) launch concurrent animations and (b) to fade out a control?
Thanks very much for any help.
[UIView animateWithDuration:duration
delay: 0.0
options: UIViewAnimationOptionCurveEaseIn +
UIViewAnimationOptionCurveEaseOut + UIViewAnimationOptionBeginFromCurrentState
animations:^{
[self Layout_Set];
}
completion:^(BOOL finished){}];
[UIView animateWithDuration:duration
delay: 0.0
options: UIViewAnimationOptionCurveEaseIn +
UIViewAnimationOptionCurveEaseOut + UIViewAnimationOptionBeginFromCurrentState
animations:^{
[self.UIImageQRCameraLogo setHidden:true];
}
completion:^(BOOL finished){}];
I have a translate animation, moving my button from A to B.
That route looks like this:
1) Button slowly accelerates
2) At the middle of animation gets to the peak of it's speed
3) Slows down as it approaches the end
This is my code
[UIView animateWithDuration:speed
delay:delay
options:UIViewAnimationOptionAllowUserInteraction
animations:^(void){
[UIView setAnimationRepeatCount:5];
cloud.frame = (CGRectMake(cloud.frame.origin.x,
cloud.frame.origin.y+900, cloud.frame.size.width, cloud.frame.size.height));
}completion:nil];
I want my animation to have a same speed all the time.
How can I achieve this?
Add the UIViewAnimationOptionCurveLinear to your animation options, like this:
[UIView animateWithDuration:speed
delay:delay
options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveLinear
animations:^(void){
[UIView setAnimationRepeatCount:5];
cloud.frame = (CGRectMake(cloud.frame.origin.x,
cloud.frame.origin.y+900, cloud.frame.size.width, cloud.frame.size.height));
}completion:nil];
I think you wanna try setting the "option" parameter to:
UIViewAnimationOptionCurveLinear
I've made something like:
[UIView animateWithDuration:2.0
delay:0
options:UIViewAnimationOptionRepeat
animations:^{
NSLog(#"Repeating...");
/** code **/
}
completion:^(BOOL finished) {
/** code **/
}];
But the animation does not repeat itself.
Am I missing something?
I've removed the color animation, but the animation doesn't repeat yet. But I must be doing something really wrong, 'cause I tried the following simple code and the animation still doesn't repeat:
[UIView animateWithDuration:1.0f
delay:0
options: UIViewAnimationOptionRepeat
animations:^{
NSLog(#"Repeating...");
self.alpha = 1;
}
completion:nil];
Depending on the animation (still would like to see your code) you might have to reverse your animation in order for it to repeat. For example, if your animation changes an alpha value from 0 to 1, repeating that, without reversing the alpha back to 0, isn't going to do much. You may need to add UIViewAnimationOptionAutoreverse to your animation options.
I have a view which I would like to transform in two ways. First I'd like to move it on its y-axis. Then, I'd like to zoom in on it.
However, when I use the following code, the object is first moved and then moved back to its original position while being zoomed.
Is there a way to apply the two transformation at once without cancelling the first?
Sorry if this is basic, but any help would be very much appreciated!
[UIView animateWithDuration:animationDuration
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
currentCover.transform = CGAffineTransformMakeTranslation(0, 0-keyboardTop+35);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:animationDuration
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
[currentCover setTransform:CGAffineTransformMakeScale (1.3, 1.3)];
}
completion:^(BOOL finished) { }
];
}
];
You should multiply one transform by the other. Each transform (scale and translate) are transform matrices. To combine them, simply multiple one by the other before using it. The order of the multiplication determines the order that the tranforms are applied
I'd similar issue where my view moves back to original position.
Documentation of CGAffineTransformMakeTranslation reads "it constructs a new translation matrix from x and y values that specify how much to move the origin." I don't think it applies actual transformation.
I think you'd rather apply translation by calling CGAffineTransformTranslate(). At least it works for me!!
Here we are creating the two changes (CGAffineTransform's) you want to do together, and apply the transformation to the view.
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 0-keyboardTop+35);
CGAffineTransform scale = CGAffineTransformMakeScale(1.3, 1.3);
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
[UIView animateWithDuration: animationDuration
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
currentCover.transform = transform;
}completion:^(BOOL finished){
}];
I am moving a UIImageView with the following animation:
[UIView animateWithDuration:.5 delay:0 options:(UIViewAnimationCurveLinear | UIViewAnimationOptionAllowUserInteraction)
animations:^{
myImage.frame = CGRectOffset(myImage.frame, myImageOffset.x, myImageOffset.y);
}
completion:^(BOOL finished){
}
];
The problem is the image starts moving slow, then speeds up and slows down again before ending the animation. Isn't the UIViewAnimationCurveLinear option supposed to make the animation perform at a constant rate?
Try using this option instead - UIViewAnimationOptionCurveLinear. I found it while looking at this question on another site - http://www.iphonedevsdk.com/forum/iphone-sdk-development/89009-block-animation-ignores-uiviewanimationcurvelinear.html