とりわけ、その下にあるものを特定の色に着色する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」の関数に追加すると、これは問題なく機能するようです(これにより、最終的に気付くのにかなりの頭痛の種になりました)。誰かが私が間違っていることを知っていますか?これを行うことさえ可能ですか、それとも私はそれに別の方法でアプローチする必要がありますか?