iphone - 将图层阴影裁剪为宽度而不是高度?

标签 iphone objective-c ios core-graphics

我正在向 View 层添加阴影,如下所示:

    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 量,但这会稍微扰乱底部的阴影流。

有什么想法可以实现这一点吗?

最佳答案

您可以添加新的“容器” View 并将您的 View (内容 View )添加为 subview 。容器 View 应高于您的 View ,但宽度相同。如果将容器 View 设置为剪切到其边界,它将剪切侧面的阴影,但允许底部和顶部的阴影。

 _________  
| _______ |  <-- 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];

关于iphone - 将图层阴影裁剪为宽度而不是高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10696042/

相关文章:

iphone - CoreBluetooth 在其他设备要求配对时触发一种方法?

ios - 如何在 Objective-C 中使自定义类线程安全

ios - 显示 pList 中特定的 "categories"

ios - 在 UITableViewCells 上显示 'Copy' 弹出窗口的简单方法,如地址簿 App

iphone - 核心数据切片绘图样式

iphone - 如何在iphone应用程序中单击按钮时隐藏删除 subview

ios - obj-c 项目中的 PagingMenuController swift 库错误

objective-c - 定期启动/唤醒 iOS 应用程序

iphone - 如何将文件从 S3 下载到 iPhone 应用程序?

ios - 将保存的上下文从 AppDelegate 加载到另一个 ViewController