0

So I have this code to add some instructions to my app's gameView:

UILabel *instructions = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, self.gameView.frame.size.width, 100)];
[instructions setTextColor:[UIColor whiteColor]];
[instructions setText:@"Some Instructions"];
[self.gameView addSubview:instructions];

NSLog(@"About to animate!");

[UIView animateWithDuration:2.0
                      delay:2.0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{ [instructions setAlpha:0.0];}
                 completion:^(BOOL fin) {NSLog(@"done");}];

In the console I can see the messages About to animate! and done but the UILabel is never visible, also the two messages appear simultaneously, not after a 4 second gap.

My understanding was that this animation would display the UILabel with its default alpha value for 2 seconds, and then fade the alpha value down to 0 over the next two seconds. I have similar animation code for UIImageViews in other places in my code.

James
  • 3,957
  • 4
  • 37
  • 82
  • http://stackoverflow.com/questions/12448847/calayer-opacity-animation – Shubhank Mar 07 '14 at 16:19
  • 3
    You've set the alpha to 0. So of course it wouldn't be visible. Did you mean to set it to 1? – Albert Renshaw Mar 07 '14 at 16:19
  • @Kabira I was just about to suggest that :) until I noticed his alpha is 0. Alpha should work just as layer.opacity should work! – Albert Renshaw Mar 07 '14 at 16:20
  • But my understanding was that this animation would display the `UILabel` with its default alpha value for 2 seconds, and then fade the alpha value down to 0 over the next two seconds. I have similar animation code for UIImageViews in other places in my code. Also there's no time gap between the two log messages. – James Mar 07 '14 at 16:27
  • I ran your code and it just works for me. What is the background color of your self.gameView? – GenieWanted Mar 07 '14 at 16:29
  • background color of `self.gameView` is `[UIColor blueColor]` – James Mar 07 '14 at 16:30
  • I think you might be assuming that UILabel is visible in the first place. Check it's frame after you create it. – Aaron Mar 07 '14 at 16:32
  • Thanks for the suggestion, I have checked that the frame is visible without the animation, it sits across the top of my gameView just where I was wanting it, so shouldn't be a problem. – James Mar 07 '14 at 16:34
  • 1
    if you are calling this instructions in viewDidLoad this may be the reason. i tried calling code in viewDidAppear it just worked fine but in viewDidLoad, it doesnt work – meth Mar 07 '14 at 17:04
  • Thanks, that fixed it. If you write up your answer below I'll up vote and accept it. I was beginning to get very frustrated with that. – James Mar 07 '14 at 17:11
  • set a red background color instructions and change delay to 10 in order to see if instructions is well visible. – thierryb Mar 07 '14 at 19:50

0 Answers0