使用子视图按钮淡化UIView

时间:2012-07-04 14:16:43

标签: xcode ios5 uiview fading icarousel

我正在尝试创建一个淡出的按钮,然后在我打开页面时进入。

这是我正在使用的当前代码,它无法淡入/淡出按钮:

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

我也尝试过使用:

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

这些都不起作用。有没有人有关于如何淡化子视图的任何提示?感谢

2 个答案:

答案 0 :(得分:2)

当您调用[UIView commitAnimations]时,动画会同时进行动画制作。 当您想要在其他动画之后为某些动画设置动画时,请尝试使用完成功能嵌套动画块:

[UIView animateWithDuration:0.5 animations:^
{
   //animate out
}
completion:^ (BOOL finished)
{
    [UIView animateWithDuration:0.5 animations:^
    {
       //animate in
    }
    completion:^ (BOOL finished)
    {
      //Optional
    }];
}];

答案 1 :(得分:1)

要淡出,您必须将alpha设置为0,而不是1。

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

这应该有用。