0

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

这是我正在使用的当前代码,淡入/淡出按钮不起作用:

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

这些都不起作用。有人对如何淡出子视图有任何提示吗?谢谢

4

2 回答 2

2

当您调用 [UIView commitAnimations] 时,动画同时进行动画处理。当您想在其他动画之后为某些内容设置动画时,请尝试使用完成嵌套动画块:

[UIView animateWithDuration:0.5 animations:^
{
   //animate out
}
completion:^ (BOOL finished)
{
    [UIView animateWithDuration:0.5 animations:^
    {
       //animate in
    }
    completion:^ (BOOL finished)
    {
      //Optional
    }];
}];
于 2012-07-04T14:23:39.953 回答
1

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

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

这应该有效。

于 2012-07-04T14:18:17.410 回答