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.
Related
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.
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 am doing a very simple repeating animation to fade a label in and out, seen below. I assumed the completion block would be called every time the animation finishes, but when using UIViewAnimationOptionRepeat it is never called. So how am I supposed to stop this animation?
I know I can use [self.lbl.layer removeAllAnimations];, however that ends its very abruptly. I want to know when it has finished a cycle of the animation so I can stop it at that time.
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut animations:^{
self.lbl.alpha = 0;
} completion:^(BOOL finished){
if (finished) NSLog(#"done");
}];
If you want the animation with the UIViewAnimationOptionRepeat option to be finite, you have to set the repeat count via + (void)setAnimationRepeatCount:(float)repeatCount of UIView.
In case of block-based animation (that is your case), you should set repeat count from within animation block. So, here is your code modified:
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut animations:^{
[UIView setAnimationRepeatCount:4]; // 4 as example here
self.lbl.alpha = 0;
} completion:^(BOOL finished){
if (finished) NSLog(#"done");
}];
This is mentioned in documentation of setAnimationRepeatCount: method of UIView.
maybe this kind of solution using a selector can help you:
- (void) animateTextWithMax:(NSNumber *)max current:(NSNumber *)current
{
NSLog(#"max = %d, current = %d",max.intValue, current.intValue);
textlabel.alpha = 1.0f;
[UIView animateWithDuration:1.0f delay:0 options:UIViewAnimationOptionAutoreverse
animations:^{
textlabel.alpha = 0.0f;
}completion:^(BOOL finished){
NSLog(#"finished");
if (current.intValue < max.intValue) {
[self performSelector:#selector(animateTextWithMax:current:) withObject:max withObject:[NSNumber numberWithInteger:(current.intValue+1)]];
}
}];
}
then you can call the animation by this way:
[self animateTextWithMax:[NSNumber numberWithInt:3] current:[NSNumber numberWithInt:0]];
Maybe this is not the best solution because you are not using the UIViewAnimationOptionRepeat option, but I think it can work.
I hope this helps you.
As you've discovered, the completion block is not called, because the animation never ends. Off the top of my head, you've got two options:
Add another animation to the label, setting it to your desired final value, with the UIViewAnimationOptionBeginFromCurrentState option. I'm not sure what effect this has on the existing, repeating animation, though. If that doesn't work...
Get the current position of your animation by examining the label.layer.presentationLayer. Stop all animations, set your value to the current state, then add a new animation to transition from the current state to the desired final state.
I use animationWithDuration for animating elements. An I have 1 animation inserted in completion block of another.
[UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
} completion:^(BOOL finished) {
... //and simple
}];
}];
So I have Stairs of six - nine elements of blocks of animationWithDuration.So how I can optimize this?
Sorry for my English.
If you have many nested animations the code quickly becomes very unreadable.
So the approach here is maybe to hold an array of all animation blocks and to iterate through them without nesting them.
One possible example how this looks in code is shown here:
Multiple UIView Animations Without Nested Blocks
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