0

ファイルの 1 つからプログラムで新しい UIView を作成し、.m5 秒後に既存のビューに戻ろうとしています。これは私が望んでいることをしていないため、私の論理はオフになっているようです。私のコードは以下です。

UIView *mainView = self.view;

UIView *newView = [[UIView alloc] init];
newView.backgroundColor = [UIColor grayColor];
self.view = newView;

sleep(5);
self.view = mainView;

5秒間スリープしてから何もしないようです。

私は次のことをしたい、

  • ストア開始ビュー
  • 新しいビューを作成
  • 灰色のビューを表示
  • 5 秒待ちます
  • 元のビューを表示

どこが間違っていますか?それは私の論理である必要があるか、これらの手順の重要な部分が欠けているように感じます.

助けてくれてありがとう!:)

4

3 に答える 3

0

GCD を使用すると、はるかに読みやすいコードが得られますが、最終的には好みの問題です。

// Create grayView as big as the view and add it as a subview
UIView *grayView = [[UIView alloc] initWithFrame:self.view.bounds];
// Ensure that grayView always occludes self.view even if its bounds change
grayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
grayView.backgroundColor = [UIColor grayColor];
[self.view addSubview:grayView];
// After 5s remove grayView
double delayInSeconds = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [grayView removeFromSuperview];
});
于 2013-06-04T20:35:14.353 に答える