CoreGraphics 透明レイヤー(物体を同一レイヤーに配置)

日本語で説明するのが, かなり苦労しそうな題名。

何がいいたいかと申しますと, AppleのDocumentでは, Transparency Layers

として, すばらしい例が掲載されていますが, レイヤーという概念で同じレイヤーに描画されたものは同じz座標ですよ(何いってんだ)という概念です。

物体を順々に描画していけば後から描画されたものが上に配置され当然その影が下の物体に描画される訳ですが, Appleの例のように同じ層に配置されるので影は出ないというかぴったりくっついているという
イメージになります。

Appleのサンプルを動く形にしてみました。ほかの私のサンプルを参考にしながらどうぞ動かしてみてください

TransparencyView.m

#import "TransparencyView.h"

@implementation TransparencyView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGFloat wd = rect.size.width;
    CGFloat ht = rect.size.height;
    
    CGContextSetRGBFillColor (context, 1, 1, 1, 1);
    CGContextFillRect (context, rect);
    
    CGSize  myShadowOffset = CGSizeMake (10, -20);
    CGContextSetShadow (context, myShadowOffset, 10);
    CGContextBeginTransparencyLayer (context, NULL);
    CGContextSetRGBFillColor (context, 0, 1, 0, 1);
    CGContextFillRect (context, CGRectMake (wd/3+ 50,ht/2 ,wd/4,ht/4));
    CGContextSetRGBFillColor (context, 0, 0, 1, 1);
    CGContextFillRect (context, CGRectMake (wd/3-50,ht/2-100,wd/4,ht/4));
    CGContextSetRGBFillColor (context, 1, 0, 0, 1);
    CGContextFillRect (context, CGRectMake (wd/3,ht/2-50,wd/4,ht/4));
    CGContextEndTransparencyLayer (context);
}

@end

CGContextBeginTransparencyLayer でスタート, CGContextEndTransparencyLayer で終了です。3つの物体とその影を, 描画しました。

結果はこうなります。
transparency-layer