【问题标题】:Activity Indicator not starting animation straight away活动指示器没有立即开始动画
【发布时间】:2011-12-15 14:25:52
【问题描述】:

我初始化一个活动指示器,并在一个按钮按下动作中启动它并调用下一个视图来显示。

-(IBAction) downloadButtonPressed:(id)sender {

    NSLog(@"Download Button Pressed");
    indicator.hidden = NO;
    [indicator startAnimating];

    if (addviewcontroller == nil)
        addviewcontroller = [[AddViewController alloc]init];
    [self.view addSubview:addviewcontroller.view];

    [addviewcontroller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentModalViewController:addviewcontroller animated:YES];

}

当我按下按钮时,活动指示器不会立即启动。它在调用另一个视图时开始。指示器会显示一秒钟,但是当按下按钮时,加载另一个视图需要一些时间。

我不知道为什么指示灯显示一秒钟没有启动。

【问题讨论】:

  • 为什么要添加两次addviewcontroller?您可以尝试以其他方法创建您的 addviewcontroller。在动作 (downloadButtonPressed) 内部简单地显示指示器。然后你可以在一些延迟后执行选择器 (performSelector:withObject:afterDelay:)。在该选择器中,您可以隐藏您的活动指示器并创建您的 addviewcontroller。
  • @Flex_Addicted 就像你和 niko 说的那样工作。非常感谢....

标签: iphone uiactivityindicatorview


【解决方案1】:

试试这个:

-(IBAction) downloadButtonPressed:(id)sender 
{
    NSLog(@"Download Button Pressed");
    indicator.hidden = NO;
    [indicator startAnimating];
    [self performSelector:@selector(showController) withObject:nil afterDelay:0.1f];
}

- (void)showController {

    if (addviewcontroller == nil)
        addviewcontroller = [[AddViewController alloc]init];
    [self.view addSubview:addviewcontroller.view];

    [addviewcontroller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentModalViewController:addviewcontroller animated:YES];
}

这应该可以解决问题 ;-)

编辑

我刚刚注意到您的代码中存在问题,您将 addviewcontroller 添加了两次。一种是通过将其添加为实际视图控制器的子视图,另一种是通过模态呈现另一个视图控制器。您应该从此函数中删除其中一条语句。

【讨论】:

  • 为什么不 afterDelay:0.00001f ?
  • 应该也可以。重要的是指标将在函数 downlaodButtonPressed 结束时开始动画。所以你必须延迟 presentModalViewController 的启动。
  • 谢谢 niko,正如你所说,我从问题 [link] stackoverflow.com/questions/7710174/… 中得到了答案。再次感谢 Niko。
  • @Niko 我按照你说的尝试。当我删除子视图时它可以工作。但是我需要提供过渡样式,所以我可以删除子视图吗?那里有什么问题。
  • @Bala : 没问题,去掉addSubview就行了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多