iOS : How to add drop shadow and stroke shadow on UIView?

标签 ios objective-c uiview dropshadow strokeshadow

我想在 UIView 上添加drop shadowstroke shadow 这是我的设计师给我的阴影,

  • 对于投影,他告诉我使用 RGB(176,199,226),不透明度为 90%,距离为 3,大小为 5

  • 对于描边阴影,他要求应用大小为 1 的 RGB(209,217,226) 和 100% 不透明度。

这是 photoshop 应用的效果屏幕,

enter image description here enter image description here

具有所需阴影的 View (预期输出)

enter image description here

我尝试了以下方法来获得投影

viewCheck.layer.masksToBounds = NO;
viewCheck.layer.cornerRadius = 5.f;
viewCheck.layer.shadowOffset = CGSizeMake(.0f,2.5f);
viewCheck.layer.shadowRadius = 1.5f;
viewCheck.layer.shadowOpacity = .9f;
viewCheck.layer.shadowColor = [UIColor colorWithRed:176.f/255.f green:199.f/255.f blue:226.f/255.f alpha:1.f].CGColor;
viewCheck.layer.shadowPath = [UIBezierPath bezierPathWithRect:viewCheck.bounds].CGPath;

这是它的结果,

enter image description here

我在理解如何将描边和阴影应用到 photoshop 屏幕截图中时遇到问题(我在上面添加了)。 Distance, Spread, Size, Position 如何应用?

有人可以指点我应用这些阴影的指南吗?出现了很多阴影效果,我想了解它是如何实现的,而不是在这里问每个问题!

谢谢:)

最佳答案

我相信您寻找的大部分配置都可以通过使用 shadowPath 属性来实现。

viewCheck.layer.shadowRadius  = 1.5f;
viewCheck.layer.shadowColor   = [UIColor colorWithRed:176.f/255.f green:199.f/255.f blue:226.f/255.f alpha:1.f].CGColor;
viewCheck.layer.shadowOffset  = CGSizeMake(0.0f, 0.0f);
viewCheck.layer.shadowOpacity = 0.9f;
viewCheck.layer.masksToBounds = NO;

UIEdgeInsets shadowInsets     = UIEdgeInsetsMake(0, 0, -1.5f, 0);
UIBezierPath *shadowPath      = [UIBezierPath bezierPathWithRect:UIEdgeInsetsInsetRect(viewCheck.bounds, shadowInsets)];
viewCheck.layer.shadowPath    = shadowPath.CGPath;

例如,通过这样设置 shadowInsets

UIEdgeInsets shadowInsets = UIEdgeInsetsMake(0, 0, -1.5f, 0);

你会得到一个漂亮的理想阴影:

enter image description here

现在您可以通过控制阴影路径矩形插入来决定如何构建阴影矩形。

关于iOS : How to add drop shadow and stroke shadow on UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17502082/

相关文章:

ios - 如何阻止 UITableView 单元格覆盖内容?

ios - 用户按下了 UIAlertView 上的哪个按钮?

iphone - 胎面 9 : EXC_BAD_ACCESS

ios - View 在 iOS 模拟器中被颠倒绘制,未正确检测到状态栏方向

ios - 在 UIView IOS 中按任意位置时移动带有触摸的 UIImageview

iphone - iOS开发中关于layer.shadow

ios - UIView 动画仅在呈现静态图像时触发

ios - 本地通知数据

ios - 如何在Expo中更新iOS SDK版本

iphone - CATransition如何剪辑到UIView的边界?