开发者

Trying to fade in a UIView without success

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 respond开发者_StackOverflows 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.



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];


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;
}];


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];


Here is also a good alternative with many options and easy to use.

  [secondViewController.view setAlpha:0.0];
  [UIView animateWithDuration:1.5
          delay:0.0 
          options:UIViewAnimationOptionCurveEaseIn // See other options
          animations:^{
            [secondViewController.view setAlpha:1.0];
        } 
        completion:^(BOOL finished) {
        // Completion Block
        }];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜