UIViewAnimation plays instantly, delay is ignored - ios

I have a simple UIViewAnimation that is called once my view is loaded.
However no matter what I place as the value for delay, it is ignored and the animation plays instantly.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:100.0];
[UIView setAnimationDelay:2.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(faceRight:finished:context:)];
_chooseLabelContainer.center = CGPointMake(originalCenter.x, 0 - _chooseLabelContainer.frame.size.height);
[UIView commitAnimations];
Update: As a test I've mangeled this scenario and below the non-delayed animation happens instantly, but the one within the dispatch queue animates overtime as expected!
UIView* objectToAnimate = self.hudController->_chooseLabelContainer;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
[UIView animateWithDuration:5.0 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
NSLog(#"Start Dispatch %#", NSStringFromCGPoint( objectToAnimate.center ) );
objectToAnimate.center = CGPointMake(objectToAnimate.center.x, objectToAnimate.center.y+90);
}completion:^(BOOL done){
NSLog(#"Done Dispatch");
}];
});
[UIView animateWithDuration:5.0 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
NSLog(#"Start %#", NSStringFromCGPoint( objectToAnimate.center ) );
objectToAnimate.center = CGPointMake( objectToAnimate.center.x, objectToAnimate.center.y+30);
}completion:^(BOOL done){
NSLog(#"Done");
}];

This might not solve your problem, but as of iOS 4 Apple recommends you use UIView animation blocks:
[UIView animateWithDuration:100.0 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_chooseLabelContainer.frame = CGPointMake(originalCenter.x, 0 - _chooseLabelContainer.frame.size.height);
}completion:^(BOOL done){
[self faceRight:finished:context]; //will of course generate errors since I don't know what arguments to pass.
}];

Related

Recursive animateWithDuration: animations: completion: method iOS

I am attempting to make my item "glow" every 3 seconds changing its alpha from a high to low value and back. It however "flickers" and basically changes every 0.2 seconds from color to color randomly. I have pasted the method below. Its probably something to do with when completion is called but not sure?
-(void)showLoading{
[self.postTableView setHidden:YES];
self.isLoading = true;
//Disappear
[UIView animateWithDuration:1.5 animations:^(void) {
self.loadingLabel.alpha = 0.8;
self.loadingLabel.alpha = 0.3;
}
completion:^(BOOL finished){
//Appear
[UIView animateWithDuration:1.5 animations:^(void) {
self.loadingLabel.alpha = 0.3;
self.loadingLabel.alpha = 0.8;
}completion:^(BOOL finished2){
if(true){
[self showLoading];
}
}];
}]; }
self.loadingLabel.alpha = .8;
[UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
self.loadingLabel.alpha = .3;
} completion:nil];
UIViewAnimationOptionAutoreverse & UIViewAnimationOptionRepeat can help you.
you can stop it by
self.loadingLabel.alpha = .8;
or the other animation.
[UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
self.loadingLabel.alpha = .8;
} completion:nil];
UIViewAnimationOptionBeginFromCurrentState is a useful option for you to stop your animation by the other animation.
-(void)viewDidLoad{
//use schedule timer to call repeatedly showLoading method until loading done
}
-(void)showLoading{
[self.postTableView setHidden:YES];
self.isLoading = true;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.loadingLabel.alpha = 0.3;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
self.loadingLabel.alpha = 0.8;
[UIView commitAnimations];
}

Different animation duration for subviews of an UIView

I have the following code
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
mainPage.frame=CGRectMake(0, -[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height,[UIScreen mainScreen].bounds.size.width);
} completion:nil];
Is there any way to control the animation duration of the subviews of mainPage.
You can use UIView:animateKeyframesWithDuration then use UIView:addKeyframeWithRelativeStartTime to set its start time and duration
[UIView animateKeyframesWithDuration:2 delay:0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0.32 relativeDuration:0.68 animations:^{
view.frame = frame;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:1 animations:^{
subview.frame = frame2;
}];
} completion:^(BOOL finished) {
}];
If you want the subviews to animate with a different duration then you'll need to animate each of them separately. You can loop through the mainPage.subviews array and issue an animateWithDuration:delay:options:animations:completion: call to each subview in turn, each with a different duration.

UIView Animation Move

I have managed to make the "view" move once. I need for it to stay 1 second in same place and then move back again to initial place. How would I add the rest of the code for this? (This won't be a loop, only done once)
[UIView animateWithDuration:0.8 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[infoView setCenter:CGPointMake(160,30)];
} completion:nil];
Use the completion block to do another animation:
[UIView animateWithDuration:0.8 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[infoView setCenter:CGPointMake(160,30)];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.8 delay:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[infoView setCenter:CGPointMake(orignalX, originalY)];
} completion:nil];
}];

How to animate a UILabel after another

I currently animate two UILabel as such:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
[_temperatureLabel setAlpha:1];
[_tempDescriptionLabel setAlpha:1];
[UIView commitAnimations];
However, I want to show the first label _temperatureLabel then once that is done animating (or maybe halfway through) start animating the second label _tempDescriptionLabel.
as I said I'll answer:
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
//set alpha 1 for first UILabel
_temperatureLabel.alpha = 1;
} completion:^(BOOL finished){
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
//when finished enter here
//set alpha 1 for second UILabel
_tempDescriptionLabel.alpha = 1;
} completion:^(BOOL finished){
}];
}];
remember to add QuartzCore framework, and add #import <QuartzCore/QuartzCore.h>
For doing halfWay or any other mid way time,
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
[_temperatureLabel setAlpha:1];
[UIView commitAnimations];
[self performSelector:#selector(halfWayStart:) withObject:nil afterDelay:1.0];
-(void)halfWayStart:(id)object{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
[_tempDescriptionLabel setAlpha:1];
[UIView commitAnimations];
}
so, changing the afterDelay time value in performSelector call will help you to start other animation any time.

Repeat Fade Effect not working

Following is my code, Used for showing some fade effect while adding the table on a sub view. Problem is that code works for the first time. After adding table, when I hit close the Sub view button table is removed. But when I hit the Button to add table again on subview fade effect doesn't work fine
double delayInSeconds2 = 0.1;
dispatch_time_t popTime2 = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds2 * NSEC_PER_SEC));
dispatch_after(popTime2, dispatch_get_main_queue(), ^(void){
self.otlTableRightView.frame=CGRectMake(100, 68, 300, 378);
[self.otlTableRightView setAlpha:0.0];
[self.otlRightFromView addSubview:self.otlTableRightView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.2];
self.otlTableRightView.frame=CGRectMake(43, 68, 300, 378);
[UIView commitAnimations];
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[self.otlTableRightView setAlpha:0.0];
}completion:^(BOOL done){
}];
});
and I am using
[self.otlTableRightView removeFromSuperview];
to remove my table from my sub view
If you want repeat animation you shouldn't removeFromSuperview your subview! Then all the repeat method does not matter.
You also may consider simpler solution for that kind of behavior:
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.otlTableRightView setAlpha:0.0];
}
completion:^(BOOL done){
[self.otlTableRightView setAlpha:2.0];
}];
As you can see in option you can add multiple parameters.
To remove your table view with fade effect try this..Hope it wold work.
> [UIView animateWithDuration:1.0
> delay:0.0
> options: UIViewAnimationOptionCurveEaseInOut
> animations:^{
> [self.otlTableRightView setAlpha:0.0];
> }
> completion:^(BOOL done){
> [self.otlTableRightView removeFromSuperview];
> }];
After some search, Finally I found something that works in my case. Here, otlTableRightView is the outlet for my UItableView
self.otlTableRightView.alpha = 0;
[UIView beginAnimations:#"fade in" context:nil];
[UIView setAnimationDuration:2.0];
self.otlTableRightView.alpha = 1.0;
[UIView commitAnimations];

Resources