2

とりわけ、その下にあるものを特定の色に着色するUIViewサブクラスを作成したいと思います。これは私が思いついたものですが、残念ながら正しく機能していないようです。

#import <UIKit/UIKit.h>

@class MyOtherView;
@interface MyView : UIView
{
    MyOtherView *subview;
}
@end

@implementation MyView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        frame.origin.x += 100.0;
        frame.origin.y += 100.0;
        frame.size.width = frame.size.height = 200.0;
        subview = [[MyOtherView alloc] initWithFrame:frame];
        [self addSubview:subview];
        [subview release];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    // Draw a background to test out on
    UIImage *image = [UIImage imageNamed:@"somepic.png"];
    [image drawAtPoint:rect.origin];

    const CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blueColor] setFill];
    rect.size.width = rect.size.height = 200.0;
    CGContextFillRect(ctx, rect);
}

@end

@interface MyOtherView : UIView
@end

@implementation MyOtherView

- (void)drawRect:(CGRect)rect
{
    // This should tint "MyView" but it doesn't.
    const CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    CGContextSetBlendMode(ctx, kCGBlendModeScreen);
    [[UIColor redColor] setFill];
    CGContextFillRect(ctx, rect);
    CGContextRestoreGState(ctx);
}

@end

「MyOtherView」を「MyView」の重なり部分を赤に着色したいのですが、代わりに不透明な赤いブロックを描画します。ただし、「MyOtherView」から-drawRect:関数をコピーして、「MyView」の関数に追加すると、これは問題なく機能するようです(これにより、最終的に気付くのにかなりの頭痛の種になりました)。誰かが私が間違っていることを知っていますか?これを行うことさえ可能ですか、それとも私はそれに別の方法でアプローチする必要がありますか?

4

2 に答える 2

0

この SO questionを確認してください。

于 2009-08-17T06:14:01.393 に答える
0

あなたはこれを考えすぎていると思います。1 つのビューを別のビューに重ね、トップ ビューのアルファを 0.5 に設定します。opaque を NO に設定する必要もあります。

ビューの背景色を適切に設定すれば、drawRect をオーバーライドする必要さえありません。

于 2009-08-17T06:09:08.143 に答える