5

パスにグロー効果を追加したいと考えています。たとえば、フォーカスがあるときに (OS X) インターフェイス要素の周りに青いグローが表示されるようにします。

(長方形の)パスで CAShapeLayer を使用しました。

self.borderLayer = [CAShapeLayer layer];
CGPathRef path = CGPathCreateWithRect(self.bounds, NULL);
[self.borderLayer setPath:path];
CGPathRelease(path);

最後に、これにより、周囲に境界線がある透明な UIView が得られます。(私の具体的なケースでは、アニメーションが追加された破線ですが、この特定の質問には関係ありません)

CALayer のシャドウ プロパティをいじってみましたが、常にレイヤー全体を塗りつぶします。

self.borderLayer.shadowPath = self.borderLayer.path;
self.borderLayer.shouldRasterize = YES;

私が望むのは、UIView の周囲の線だけが影を落とし、UIView の内部が透明なままになることです。

4

2 に答える 2

3

グローではなく、必要な場所にシャドウが表示されるのと同様の問題がありました。2つのCALayerを使用して解決しました。1つは、コード内の背景用の「_bg」(私の場合、不透明度0.55の黒)と白い境界線です。コード '_shadow' 内のもう 1 つのレイヤーは、背景が透明で、グロー効果が追加されています。_bg は _shadow レイヤーのサブビューです。関連するコードは次のとおりです。

_bg = [CALayer layer];
_shadow = [CALayer layer];

[self.layer insertSublayer:_shadow atIndex:0];
[_shadow addSublayer:_bg];

_bg.frame = self.bounds;
_bg.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:.55].CGColor;
_bg.cornerRadius=20.0;
_bg.borderColor=[UIColor whiteColor].CGColor;
_bg.borderWidth=2.0;

_shadow.frame=self.bounds;
_shadow.masksToBounds=NO;
_shadow.backgroundColor = [UIColor clearColor].CGColor;
_shadow.cornerRadius=3.0;
_shadow.shadowRadius=3.0;
_shadow.shadowColor=[UIColor whiteColor].CGColor;
_shadow.shadowOpacity=0.6;
_shadow.shadowOffset=CGSizeMake(0.0, 0.0);
于 2013-05-08T16:18:33.310 に答える
2

このようなことを試すことができます:</p>

    //background layer of the shadow layer
    contentViewBackgroundLayer = [CALayer layer];
    contentViewBackgroundLayer.frame = contentView.bounds;
    contentViewBackgroundLayer.backgroundColor = someColor.CGColor;
    //shadow layer
    contentViewShadowLayer = [CALayer layer];
    [contentViewShadowLayer addSublayer:contentViewBackgroundLayer];
    contentViewShadowLayer.backgroundColor = [UIColor clearColor].CGColor;
    //shadowRadius
    contentViewShadowLayer.shadowRadius = 10.0;
    contentViewShadowLayer.shadowColor = [UIColor blackColor].CGColor;
    contentViewShadowLayer.shadowOpacity = 0.5;
    contentViewShadowLayer.shadowOffset = CGSizeMake(0.0, 0.0);
    //Important!!!!  add mask to shadowLayer
    contentViewBackgroundLayer.frame = contentView.bounds;
    contentViewShadowLayer.frame = contentView.bounds;
    CGRect rect = CGRectMake(contentViewShadowLayer.bounds.origin.x - 20, contentViewShadowLayer.bounds.origin.y - 20, contentViewShadowLayer.bounds.size.width + 40, contentViewShadowLayer.bounds.size.height + 40);
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
    [path appendPath:[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, contentView.bounds.size.width, contentView.bounds.size.height)]];
    //a rectangle which is transparent surrounded by a bigger rectangle
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.fillRule = kCAFillRuleEvenOdd;
    shapeLayer.path = [path CGPath];
    contentViewShadowLayer.mask = shapeLayer;
    [contentView.layer insertSublayer:contentViewShadowLayer atIndex:0];
于 2015-02-04T13:05:32.557 に答える