Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to fade in a UIView without success

Tags:

I am trying to fade in a UIView as a subview of my main view. The UIView I am trying to fade in has the dimensions of 320x55.

I setup the view and a timer;

secondView.frame = CGRectMake(0, 361, 320, 55);
secondView.alpha = 0.0;
[self.view addSubview:secondView];
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(fadeView) userInfo:NO repeats:NO];

The timer triggers the following code;

secondView.alpha = 1.0;
CABasicAnimation *fadeInAnimation;
fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.duration = 1.5;
fadeInAnimation.fromValue = [NSNumber numberWithFloat:0.0];
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
[fadeInAnimation setDelegate:self];
[secondView.layer addAnimation:fadeInAnimation forKey:@"animateOpacity"];

My secondView is connected in Interface Builder and responds to other messages but I can't see anything happening on screen.

Can anyone please help me figure out what's going on here?

Thanks, Ricky.


In reply to a following recommendation:

I'm a bit unsure here. Initially I put this code in (because I see secondView as an instance of UIView?):

[secondView beginAnimations:nil context:NULL]; 
[secondView setAnimationDuration:0.5]; 
[secondView setAlpha:1.0]; 
[secondView commitAnimations]; 

I then tried your suggestion which didn't produce warnings or errors, but it still does brings nothing to the surface:

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[secondView setAlpha:1.0]; 
[UIView commitAnimations]; 

Thanks! Ricky.


like image 720
Ricky Avatar asked Jan 25 '10 10:01

Ricky


2 Answers

You should be able to do this a bit simpler. Have you tried something like this?

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[secondView setAlpha:1.0];
[UIView commitAnimations];
like image 135
kubi Avatar answered Sep 30 '22 07:09

kubi


it seems to me that one thing you're missing is that your view may already have an alpha of 1.0. make sure the alpha is 0 (or whatever you desire it to be) prior to the animation call.

i prefer to use block animations for this. it's cleaner and more self-contained.

secondView.alpha = 0.0f;
[UIView animateWithDuration:1.5 animations:^() {
    secondView.alpha = 1.0f;
}];
like image 45
steveb Avatar answered Sep 30 '22 08:09

steveb