在IBAction方法返回后显示UIView

时间:2016-08-06 08:12:30

标签: ios objective-c cocoa-touch

我已经获得了以下代码片段(用于说明目的):

- (IBAction)buttonPress:(id)sender {
    UIView *overlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicator.center = overlayView.center;
    [overlayView addSubview:activityIndicator];
    [self.navigationController.view addSubview:overlayView];
    //[self.navigationController.view bringSubviewToFront:overlayView];
    [activityIndicator startAnimating];

    sleep(10);

    //[overlayView removeFromSuperview];
}

我想知道为什么叠加视图仅在方法返回后显示...

1 个答案:

答案 0 :(得分:0)

我想这是因为sleep(10)阻止了负责执行UI相关活动的主线程,例如在屏幕上绘图。

删除sleep(10)行,应该可以正常工作。

如果您对如何使用可口可乐触摸的延迟感兴趣,请点击此处与SO的链接:
Adding delay between execution of two following lines