【问题标题】:Fading a UIView with a subviewed button使用子视图按钮淡化 UIView
【发布时间】:2012-07-04 14:16:43
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: xcode ios5 uiview fading icarousel


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      这应该可行。

      【讨论】:

      • 我的意思是在我原来的帖子中说,每当我尝试这样做时,就像将 alpha 设置为 0.0 一样,按钮永远不会出现。它总是隐藏的。
      猜你喜欢
      • 2012-10-01
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多