2013-05-07 56 views
4

點擊UIView後,我隱藏它並用UIView和Quartz drawRect初始化新對象。爲什麼UIImageView僅在延遲後才隱藏?

- (void)viewTapped:(UITapGestureRecognizer *)recognizer { 
    self.vignetteView.hidden=true; 
    lupeItself = [[LoupeView alloc] initWithView:_pageView setZoomImageName:_zoomPageImageName setDelegate:self]; 
} 

上面的代碼僅在延遲了2秒後隱藏了UImageView。但是如果最後一行(LoupeView alloc等)被刪除,它會立即被隱藏。爲什麼?如何立即隱藏視圖?

回答

7

.hidden = true更改在執行路徑返回主runloop之前不會變爲可見。第二行可能會阻塞幾秒鐘,防止發生這些變化(或者drawRect需要很長時間才能完成)。

最簡單的解決將是推遲第二行,直到下一個runloop迭代:

self.vignetteView.hidden = YES; 
// defer execution so the above changes are immediately visible 
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
    lupeItself = [[LoupeView alloc] initWithView:_pageView setZoomImageName:_zoomPageImageName setDelegate:self]; 
}]; 

而且,一個小點:你應該使用常量YESNOBOOL性能和參數,而不是truefalse