2016-04-04 10 views
3

以外のメインのUIViewにCAShapeLayerを適用します)も同じUIViewにあります。 できますか?はここで全体のUIViewにマスクを適用するコードがあるUIButton

UPD:私は

[self.view.layer insertSublayer:maskLayer atIndex:0]; 

代わりの

self.view.layer.mask = maskLayer; 

を試みたが、私のself.viewは、アルファチャネルを失い、マスク層のアニメーションはここ

が持つプロジェクトでもう動作しません。コード: https://www.dropbox.com/s/b94qcwxzoi23kwk/test_04092016.zip?dl=0

+0

[insertSublayer self.view.layer:勾配atIndex:0]; –

+0

このコードを追加した後、UIViewがアルファなしで黒くなり、maskLayerのアニメーションがもう機能しなくなりました。 – Sergio

+0

あなたはフルビューをマスクし、現在表示されているフルボタンフレームのみを表示したい – HardikDG

答えて

0

あなたは次のことを試してみませんか?代わりに次のスタックの:マスク•

(ビュー) - >ボタン

追加サブビューで、クリアな視界の背景色を作成し、次の操作を行います。

•ビュー - >マスク(サブビューを) 、ボタン

この方法では、同じ視覚効果が得られますが、ボトムビューはコンテナとして使用します。コードで:

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:self.view.bounds]; 
[maskPath appendPath:[UIBezierPath bezierPathWithArcCenter:self.mapCardsButton.center 
                radius:self.mapCardsButton.frame.size.height/2.0f 
               startAngle:0.0f 
                endAngle:2.0f*M_PI 
               clockwise:NO]]; 
CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.fillRule = kCAFillRuleEvenOdd; 
maskLayer.fillColor = [UIColor blackColor].CGColor; 
maskLayer.path = maskPath.CGPath; 

UIView *maskedView = [[UIView alloc] initWithFrame:self.view.bounds];  
maskedView.mask = maskLayer; 

self.view.backgroundColor = [UIColor clearColor]; 
[self.view addSubView:maskedView]; 
[self.view addSubView:self.mapCardsButton]; 
0

最も簡単な方法はUIButtonは、サブビューではなく、マスクされたビューの兄弟ビューになります。あなたのコードがおそらくUIViewControllerサブクラスのものであるように見えるので、ボタンがサブビューになるのが理にかなっていると思います。mapCardsButtonから離れた他のすべてのサブビューを保持するコンテナUIViewを作成し、この新しいビューにマスクします。

それでは、あなたは新しいビューmaskedContainerViewと呼ば言う:

  • self.viewは2つのサブビューがありますmaskedContainerViewmapCardsButton
  • maskedContainerViewself.viewmapCardsButton
  • maskedContainerView.layer.mask = maskLayer以外に使用したすべてのサブビューを保持しています。
+0

ありがとうございます。私はあなたのソリューションを試しましたが、それでも動作しません。これでmapCardsButtonが表示されますが、「穴」は作成されません。ここにコードを含むプロジェクトがあります:https://www.dropbox.com/s/b94qcwxzoi23kwk/test_04092016.zip?dl=0 – Sergio

+2

ねえ。遅れてしまい申し訳ありません。私はちょうどあなたのコードを見ました。 'maskedContainerView'には、' self.view'が使用した内容が含まれている必要があります。つまり、 'maskedContainerView'の背景色は、' self.view'が現在設定されている灰色/青色の背景色に設定されていなければなりませんに。それで 'self.view'は完全に透明でなければなりません。 'self.view.backgroundColor = [UIColor clearColor]'です。 – stefandouganhyde

+0

stefandouganhyde、すてきな回避策!私はその事件を考えていた – Sergio

0

[maskedContainerView.layer insertSublayer:マスク層atIndex:(符号なし)maskedContainerView.layer.sublayers.count]。

重要性は、それを取るしてください。 'atIndex'

success でview.layer.sublayers.count 0ではありません!

0

コードベース。私はこれがあなたが望むものであるかどうかはわかりません。 enter image description here

ボタンのUIBezierPathをmaskPathに追加するだけです。 このようなコード(コード上の塩基)

const NSInteger kHoleSize = 100; 

UIButton *mapCardsButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kHoleSize, kHoleSize)]; 
[mapCardsButton setTitle:@"BUTTON" forState:UIControlStateNormal]; 
mapCardsButton.backgroundColor = [UIColor redColor]; 

[self.view addSubview:mapCardsButton]; 



UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:self.view.bounds]; 


UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:self.view.center 
                  radius:kHoleSize/2.0f 
                 startAngle:0.0f 
                 endAngle:2.0f*M_PI 
                 clockwise:NO]; 

UIBezierPath *buttonPath = [UIBezierPath bezierPathWithRect:mapCardsButton.frame]; 
[circlePath appendPath:buttonPath]; 


[maskPath appendPath:circlePath]; 
[maskPath setUsesEvenOddFillRule:YES]; 

CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.path = maskPath.CGPath; 
maskLayer.fillRule = kCAFillRuleEvenOdd; 
maskLayer.fillColor = [UIColor blackColor].CGColor; 
maskLayer.opacity = 0.9; 

[self.view.layer addSublayer:maskLayer]; 
関連する問題