views:

719

answers:

3

Hi there.

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.


+4  A: 

This won't answer your question if you insist on using CoreAnimations, but for iPhoneOS it's much easier to use animation blocks for UIView animations.

secondView.alpha = 0.0f;
[UIView beginAnimations:@"fadeInSecondView" context:NULL];
[UIView setAnimationDuration:1.5];
secondView.alpha = 1.0f;
[UIView commitAnimations];

Also, you can invoke a delegate in delayed time with

[self performSelector:@selector(fadeView) withObject:nil afterDelay:0.5];
KennyTM
You can get rid of the timer alltogether by also calling UIView setAnimationDelay. e.g. [UIView setAnimationDelay:0.5]
Ron Srebro
A: 

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];
kubi
That code produces 3 warnings, all saying that UIView may not respond to the messages. It also crashes the app. Why is this?
Ricky
@Ricky: That's odd. Are you using the iPhone SDK?
KennyTM
Can you post the surrounding code? It compiles just fine when I do it.
kubi
Yes I'm using the iPhone SDK. See my edited original post for the formatted code.
Ricky
A: 

Woops sorry guys. The suggested replies did work - it's just my view was being clipped by another view and I wasn't able to see the animation.

Thanks again. Ricky.

Ricky