NSTimer With ActivityIndi​​cator

时间:2011-03-19 06:22:11

标签: ios nstimer uiactivityindicatorview

我想在一段时间后停止ActivityIndicator。我想如果我在一段时间后调用一个方法来停止活动指示器就可以实现这一点。

可以使用NSTimer来实现,但我不知道如何。使用代码段的示例非常棒。

1 个答案:

答案 0 :(得分:3)

.h班,

UIActivityIndicatorView *activity;

并将此代码用于您的实现类

- (void)viewDidLoad {
    [super viewDidLoad];

    //initialise activityIndicator and add it to view
    activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activity.frame = CGRectMake(150,200, 20, 20);
    [self.view addSubview:activity];
    [activity startAnimating];

    //call timer to fire after your required time
    [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(timerMethod:) userInfo:nil repeats:NO];
}

计时器操作,

-(void) timerMethod : (NSTimer *) theTimer {
    // after 1.5 seconds, the activity indicator will be hidden.
      [activity stopAnimating];
}