2011-06-01 44 views
2

問題UIView顯示順序問題?

當繪製在屏幕上(的iOS 4)的自定義的電影播放器​​的控制器,我使用AVPlayerLayer顯示量。我希望在播放器上疊加一些疊加層(廣告橫幅,播放器控件等)。目前,這個工作絕對沒問題有以下層次:

 
UIView(self.view)\ 
       | UIView(controlsView)\ 
       *      | UIView [some movie player controls] 
             | UIView [some more movie player controls] 
             * 

AVPlayerLayerself.view一個子層。在玩家負載上,我先將圖層添加到self.view,然後在controlsView上調用bringSubviewToFront:,然後檢測它上的點擊​​並以編程方式在定時器上顯示/淡出/無論如何。這裏沒有問題。

現在,我有支持動態獲取和顯示廣告內容,我想建立以下層次:

 
UIView(self.view)\ 
       | UIImageView (ad banner) 
       | UIImageView (another ad banner) 
       | ... 
       | UIView(controlsView)\ 
       *      | UIView [some movie player controls] 
             | UIView [some more movie player controls] 
             * 

在播放器的負載,我那麼做的是連接到我的AVPlayer比如有的addBoundaryTimeObserverForTimes:queue:usingBlock:來電會在適當的時候淡入淡出廣告條幅。當我這樣做時,我將UIImageView s添加爲self.view作爲沒有明確排序的子視圖(因此它們應該繪製在所有其他上面),並且setHidden:YES在其上。

我已經NSLog「d和這些褪色塊被正確執行,設置顯示view.alpha = 1.0fview.alpha = 0.0f分別隱藏在廣告條幅,但沒有什麼是出現在所有的,即使明確設定在視圖中顯示的UIImage到已知的工作局部圖像和幀到(0, 0, 320, 480),加上一個設定的

UIViewAutoresizingFlexibleTopMargin | 
UIViewAutoresizingFlexibleLeftMargin | 
UIViewAutoresizingFlexibleRightMargin; 

UIViewContentModeautoresizingMask是默認值(應爲UIViewContentModeScaleToFill)。


的「解決方案」我已經與問題

這實際上不解決問題在所有的,但我現在用0.1%的不透明度爲0的替代品%不透明度。有效隱藏元素而不會造成問題,而淡入淡出效果良好。


故障排除

爲了確保我沒有畫電影超過所有非控制的觀點上,我搬到了AVPlayerLayer一個附加視圖,videoView

 
UIView(self.view)\ 
       | UIView(videoView) 
       | UIImageView (ad banner) 
       | UIImageView (another ad banner) 
       | ... 
       | UIView(controlsView)\ 
       *      | UIView [some movie player controls] 
             | UIView [some more movie player controls] 
             * 

我明確地打電話給sendSubviewToBack:videoViewbringSubviewToFront:controlsView,但還是沒有愛。當我第一次將它們添加到視圖層次結構時,我不會撥打setHidden:YES,我可以在屏幕上看到疊加廣告。推文是這樣的:該廣告也在正確的時間(在我的動畫塊中響應overlayImage.alpha = 0.0f)消失,所以我知道我的動畫塊沒問題。

下面的代碼片段做添加廣告橫幅到我的電影播放器​​的看法:

for(MyCustomAdBannerClass *overlay in overlays) 
{ 
    UIImageView *overlayImage = 
    [[UIImageView alloc] initWithImage: 
    [UIImage initWithData: 
     [NSData dataWithContentsOfURL:[overlay contentURL]]]]; 
    UIViewAutoresizing autoresizingFlags = 
     UIViewAutoresizingNone; 
    CGFloat x, y, width, height; 
    width = [overlay width]; 
    height = [overlay height]; 
    // <snip> set x, y, autoresizing values of the above vars 
    [overlayImage setFrame:CGRectMake(x, y, width, height)]; 
    [overlayImage setAutoresizingMask:autoresizingFlags]; 
    [self.view addSubview:overlayImage]; 
} 

for(UIView *subview in self.view.subviews) 
{ 
    if(subview == controlsView) 
     [self.view bringSubviewToFront:subview]; 
    else if(subview == videoView) 
     [self.view sendSubviewToBack:subview]; 
    else 
     [subview setHidden:YES]; 
} 

下面是我用添加疊加出現/消失回調(overlay代碼是信息的對象何時展示覆蓋和多長時間,overlayImage是包含實際的廣告橫幅)的UIImageView

[self.player 
addBoundaryTimeObserverForTimes:[NSArray arrayWithObject: 
            [overlay startTime]] 
queue:dispatch_get_main_queue() 
usingBlock:^{ 
    [UIView 
     animateWithDuration:0.7 
     animations:^{ 
      overlayImage.alpha = 1.0f; 
      dispatch_time_t popTime = 
      dispatch_time(DISPATCH_TIME_NOW, 
         [overlay duration] * NSEC_PER_SEC); 
      dispatch_after(popTime, dispatch_get_main_queue(), 
         ^(void){ 
          [UIView 
           animateWithDuration:0.7 
           animations:^{ 
            overlayImage.alpha = 0.0f; 
            [overlayImage removeFromSuperview]; 
           }]; 
          }]; 
       }]; 
}]; 

正如我所說,overlayImage.alpha = 0.0f;呼叫正在執行正確

NOTE:剛剛嘗試設置overlay.alpha = 0.5f;而不是0.0f當第一次添加視圖時。現在正確疊加FADES IN。所以有一些時髦的事情使得視圖100%不可見,這阻止了它在以後再次被顯示。沒有崩潰,所以它不僅僅是引用有限的內存。


試圖解決方法

我試圖發送後面videoView的所有廣告覆蓋,然後在每一個是顯示我叫

[overlayImage removeFromSuperview]; 
[self.view insertSubview:overlayImage aboveView:videoView]; 
// dispatch hide animation 

仍未顯示他們,但當我省略setHidden:YES時仍然正常消失。

+0

隱藏和alpha屬性是完全分開的。如果你隱藏一個視圖,改變alpha將不會顯示它,直到你取消隱藏它。 – ughoavgfhw 2011-06-02 02:26:46

+0

@ughoavgfhw:其實這是不正確的。即使將方法更改爲setAlpha:0.0f而不是setHidden:YES,行爲也是相同的(沒有顯示),然後當我省略setAlpha:0.0f調用時,它會從一開始就顯示,然後在正確的時間消失。 – darvids0n 2011-06-02 03:08:03

回答

0

請注意,我現在使用0.001f而不是0.0f並且沒有顯示問題。我仍然渴望弄清楚它爲什麼會這樣做。