19
CALayer *sublayer = [CALayer layer];
/*sublayer.backgroundColor = [UIColor blueColor].CGColor;
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shadowOpacity = 0.8;*/
sublayer.frame = CGRectMake(30, 100, 256, 256);
sublayer.contents = (id)[[UIImage imageNamed:@"moon.png"] CGImage];
[self.view.layer addSublayer:sublayer];
//self.view.backgroundColor = [UIColor blackColor];
//add moon mask
UIGraphicsBeginImageContextWithOptions(CGSizeMake(400, 400), YES, 1);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextClearRect(contextRef, CGRectMake(0, 0, 400, 400));
CGContextSetRGBFillColor(contextRef, 1, 1, 1, 0.8);
CGContextSetRGBStrokeColor(contextRef, 0, 0, 0, 0.5);
CGRect ellipse = CGRectMake(50, 50, 128, 128);
CGContextAddEllipseInRect(contextRef, ellipse);
CGContextFillEllipseInRect(contextRef, ellipse);
CALayer* sublayer2 = [CALayer layer];
sublayer2.frame = CGRectMake(30, 100, 256, 256);
sublayer2.backgroundColor = [UIColor clearColor].CGColor;
sublayer2.contents = (id)[UIGraphicsGetImageFromCurrentImageContext() CGImage];
[self.view.layer addSublayer:sublayer2];
これはこれまでに書いたコードです。まず私は月の画像でレイヤーを作る。次に、私は月の一部をカバーする必要があるカスタム図面で2番目のレイヤーを追加します。しかしcontextRefはその背景を黒色にしているので、2番目のレイヤーを置くと、最初は見えません。私はこれを解決する方法はありますか?さらに、カスタム描画をプログラム的に作成してレイヤーに追加する方が良いでしょうか?Iphoneコンテキスト背景を透明にするにはどうすればいいですか?
あなたが不思議に思われる場合は、その引数は 'opaque'値です:https://developer.apple.com/reference/uikit/1623912-uigraphicsbeginimagecontextoo?language=objc – Senseful