2012-05-22 62 views
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的量,但是这会让底部的阴影流变得有点混乱。

任何想法如何实现?

回答

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]; 
相关问题