2010-08-01 60 views
0

關於第二層位圖彼此之間的問題,其中之一是半透明的。在iPhone上的子視圖背後創建一個透明的UIView

我有一個名爲foo的自定義UIView。我有兩個我創建的CGBitmapContext。它們被有效地用作離屏緩衝區並且具有相同的矩形尺寸。我想把這些屏幕中的兩個放在另一個的上面。背景應該是半透明的,前景應該是不透明的(但是使用clearColor來透視背景)。效果是,您可以清楚地看到前景未被着色的背景圖像(clearColor)的前景。

CGContextRef background; 
CGContextRef foreground; 

我想在兩個CGBitmapContext所在的自定義UIView foo中實現這個。我有一個父視圖(背景)與子視圖(前景),我想實現這(void)drawRect:(CGRect)rect覆蓋。我已經嘗試過重複這個過程,但我通常只能獲取其中一個視圖/位圖來顯示。

問題1:這是正確的做法嗎?我應該嘗試使用圖層嗎?我應該結合位圖,然後將BLT結合到父/根UIView?問題2:可以提供任何示例代碼來實現上述功能,敬請期待!

謝謝!

回答

0

UPDATE:

這是絕對荒謬的,我不得不這樣做,因爲必須有一個更簡單的方法,但是我最終定位兩個獨立UIViews了另一種,然後相應地設置不透明度和透明度。這工作。

我想現在就寫一個新的觀點,即這是否它看起來像正確的方式

UIView : myview 
    - (subview) UIImageView 
    - (subview) UIImageView 


The code looks something like this... 

@interface customView : UIView 
{ 
    ... 
    UIImageView *foregroundImage; 
    UIImageView *backgroundImage; 
    ... 
} 
@end 

@implementation 

- (void) awakeFromNib 
{ 
    ... 
    self.backgroundColor = [UIColor whiteColor]; 
    self.alpha = 1; 
    self.opaque = YES; 
    self.clearsContextBeforeDrawing = FALSE; 

    [backgroundImage initWithImage:<...>]; 
    backgroundImage.alpha = 0.5; // Yes, this should be 0.5. The background should be a bit lucid 
    backgroundImage.opaque = YES; // I want the white background of the parent UIView to show through 

    [foregroundImage initWithImage:<...>]; 
    foregroundImage.alpha = 1; // Yes, this should be 1. The surrounding space is clear... 
    backgroundImage.opaque = YES; 

    [self addSubview:backgroundImage]; 
    [self addSubview:foregroundImage]; 

    [self bringSubviewToFront:foregroundImage]; 

    [self setNeedsLayout]; 

    [foregroundImage setNeedsDisplay]; 
    [backgroundImage setNeedsDisplay]; 
    [self setNeedsDisplay]; 
    ... 
} 

- (void) drawRect: (CGRect) rect 
{ 
    ... 
    // update the images to latest 
    [backgroundImage setImage:<..new..>]; 
    [foregroundImage setImage:<..new..>]; 

    [backgroundImage setNeedsDisplay]; // necessary? 
    [foregroundImage setNeedsDisplay]; // necessary? 
    [self setNeedsDisplay]; // necessary? 
    ... 
} 
@end 

結果?沒有。沒有更新。我讀過其他人也遇到過這個問題(設置Image屬性時UIImageView沒有更新),但還沒有看到解決方案。這些圖像是由在觸摸事件被寫入到屏幕外的緩衝區等來

我知道,這些緩衝器具有正確的圖像,因爲如果我這樣做:

CGImageRef bitmap = CGBitmapContextCreateImage(bufferContext); 
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0,0,self.bounds.size.width, ...height), bitmap); 
CGImageRelease(bitmap); 

...我可以看到正確的圖像。 UIImageViews似乎不想更新。它們的不透明度等也相應設定。

會繼續磨,不想爲此寫個黑客。應該有一個簡單,優雅的方式...

任何幫助/指導表示讚賞。