0

I'm trying to create a button that fades out and then in when I open the page.

Here is the current code that i'm using, it doesn't work to fade the buttons in/ out:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    if (index == 0) {
        view = [[[NSBundle mainBundle] loadNibNamed:@"nib1" owner:self options:nil] lastObject];
    }else {
        view = [[[NSBundle mainBundle] loadNibNamed:@"nib2" owner:self options:nil] lastObject];
        UIView *containView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 60, 100)];
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn.frame = CGRectMake(30, 90, 40, 30 );
        [_btn setBackgroundImage:[UIImage imageNamed:@"playButton.png"] forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(fadeButton:) forControlEvents:UIControlEventTouchUpInside];
        //[self fadeOut:_btn withDuration:1.0 andWait:1.0];
        [containView addSubview:_btn];
        [containView setAlpha:0.0];
        [view addSubview:containView];
        [UIView beginAnimations:nil context:nil];
        [containView setAlpha:1.0];
        [UIView commitAnimations];
    }
return view;
}

I've also tried using:

[UIView animateWithDuration:0.5 animations:^{_btn.alpha = 1.0;}];

None of those work. Does anyone have any tips on how to fade a subview out? Thanks

mhorgan
  • 886
  • 2
  • 11
  • 32

2 Answers2

2

When you call [UIView commitAnimations], the animations are animating at the same time. When you want to animate something after an other animation try to nest animation blocks with completion:

[UIView animateWithDuration:0.5 animations:^
{
   //animate out
}
completion:^ (BOOL finished)
{
    [UIView animateWithDuration:0.5 animations:^
    {
       //animate in
    }
    completion:^ (BOOL finished)
    {
      //Optional
    }];
}];
Justin
  • 2,960
  • 2
  • 34
  • 48
1

To fade out, you have to animate the alpha to 0, not to 1.

[UIView animateWithDuration:0.5 animations:^{_btn.alpha = 0.0;}];

This should work.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • I meant to say in my original post, that whenever I try that, as in set the alpha to 0.0, the button just never appears. It's always hidden. – mhorgan Jul 04 '12 at 14:29