0

次のように、ビューレイヤーにシャドウを追加しています。

    self.view.layer.shadowOffset = CGSizeZero;
    self.view.layer.shadowOpacity = 0.10f;
    self.view.layer.shadowRadius = 5.0f;
    self.view.layer.shadowColor = [UIColor blackColor].CGColor;
    self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:
                                 CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width - 5.0, self.view.bounds.size.height)].CGPath;
    self.view.clipsToBounds = NO;

私がやりたいのは、シャドウが幅を超えないように、しかし高さを超えないように、どういうわけかクリップすることです。基本的に、境界全体の影ではなく、90度の影が必要です。bezierRectの幅からshadowRadiusの量を差し引いてみましたが、これにより、下部のシャドウフローが少し混乱します。

これをどのように達成できるかについてのアイデアはありますか?

4

1 に答える 1

1

新しい「コンテナ」ビューを追加し、ビュー(コンテンツビュー)をサブビューとして追加できます。コンテナビューは、ビューよりも高く、幅は同じである必要があります。コンテナビューをその境界にクリップするように設定すると、側面のシャドウはクリップされますが、下部と上部のシャドウは許可されます。

 _________  
| _______ |  <-- container
||       ||
||       ||  <-- your view (inside container)
||_______||
|`````````|  <-- shadow of your view (inside container)
|_________|

コードでは、これは次のようになります

// contentView is already created and configured...
UIView *containerView = [[UIView alloc] initWithFrame:
                                 CGRectInset([contentView frame], 
                                             0,         // keep the same width 
                                             -radius)]; // increase the height
[[self view] addSubview:containerView];
[contentView setCenter:CGPointMake(CGRectGetMidX([contentView bounds]), 
                                   CGRectGetMidY([contentView bounds]));
[containerView addSubview:contentView];
[containerView setClipsToBounds:YES];
于 2012-05-22T06:26:18.117 に答える