使用 swift 3 在 UIView 上添加阴影 [英] add Shadow on UIView using swift 3

查看:17
本文介绍了使用 swift 3 在 UIView 上添加阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前的 swift 3 我像这样在 UIView 中添加阴影:

//toolbar 是一个 UIToolbar (UIView)toolbar.layer.masksToBounds = false工具栏.layer.shadowOffset = CGSize(宽度:-1,高度:1)工具栏.layer.shadowRadius = 1工具栏.layer.shadowOpacity = 0.5

但是上面的代码在 swift 3 中不起作用,而不是阴影,我的整个视图的颜色变成了丑陋的灰色

有人知道如何在 swift 3 中添加阴影吗?

解决方案

代码片段:

扩展 UIView {//输出 1func dropShadow(scale: Bool = true) {layer.masksToBounds = falselayer.shadowColor = UIColor.black.cgColorlayer.shadowOpacity = 0.5layer.shadowOffset = CGSize(宽度:-1,高度:1)layer.shadowRadius = 1layer.shadowPath = UIBezierPath(rect: bounds).cgPathlayer.shouldRasterize = truelayer.rasterizationScale = 比例?UIScreen.main.scale : 1}//输出 2func dropShadow(color: UIColor, opacity: Float = 0.5, offSet: CGSize, radius: CGFloat = 1, scale: Bool = true) {layer.masksToBounds = falselayer.shadowColor = color.cgColorlayer.shadowOpacity = 不透明度layer.shadowOffset = offSetlayer.shadowRadius = 半径layer.shadowPath = UIBezierPath(rect: self.bounds).cgPathlayer.shouldRasterize = truelayer.rasterizationScale = 比例?UIScreen.main.scale : 1}}

<块引用>

注意:如果您没有将任何参数传递给该函数,则默认情况下 scale 参数将为 true.您可以为函数中的任何参数定义默认值,方法是在该参数的类型之后为该参数分配一个值.如果定义了默认值,则可以在调用函数时省略该参数.

输出 1:

shadowView.dropShadow()

输出 2:

shadowView.dropShadow(color: .red, opacity: 1, offSet: CGSize(width: -1, height: 1), radius: 3, scale: true)

<块引用>

layer.shouldRasterize = true 将使阴影静态并为 UIView 的初始状态产生阴影.所以我建议不要在动态布局中使用 layer.shouldRasterize = true,比如 UITableViewCell 中的视图.

prior swift 3 i was adding shadow in my UIView like this :

//toolbar is an UIToolbar (UIView)
toolbar.layer.masksToBounds = false
toolbar.layer.shadowOffset = CGSize(width: -1, height: 1)
toolbar.layer.shadowRadius = 1
toolbar.layer.shadowOpacity = 0.5

but the above code is not working in swift 3 , instead of shadow my whole View's color is turned to ugly gray

anyone knows how can we add shadow in swift 3 ?

解决方案

CODE SNIPPET:

extension UIView {

  // OUTPUT 1
  func dropShadow(scale: Bool = true) {
    layer.masksToBounds = false
    layer.shadowColor = UIColor.black.cgColor
    layer.shadowOpacity = 0.5
    layer.shadowOffset = CGSize(width: -1, height: 1)
    layer.shadowRadius = 1

    layer.shadowPath = UIBezierPath(rect: bounds).cgPath
    layer.shouldRasterize = true
    layer.rasterizationScale = scale ? UIScreen.main.scale : 1
  }

  // OUTPUT 2
  func dropShadow(color: UIColor, opacity: Float = 0.5, offSet: CGSize, radius: CGFloat = 1, scale: Bool = true) {
    layer.masksToBounds = false
    layer.shadowColor = color.cgColor
    layer.shadowOpacity = opacity
    layer.shadowOffset = offSet
    layer.shadowRadius = radius

    layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
    layer.shouldRasterize = true
    layer.rasterizationScale = scale ? UIScreen.main.scale : 1
  }
}

NOTE: If you don't pass any parameter to that function, then the scale argument will be true by default. You can define a default value for any parameter in a function by assigning a value to the parameter after that parameter’s type. If a default value is defined, you can omit that parameter when calling the function.

OUTPUT 1:

shadowView.dropShadow()

OUTPUT 2:

shadowView.dropShadow(color: .red, opacity: 1, offSet: CGSize(width: -1, height: 1), radius: 3, scale: true)

layer.shouldRasterize = true will make the shadow static and cause a shadow for the initial state of the UIView. So I would recommend not to use layer.shouldRasterize = true in dynamic layouts like view inside a UITableViewCell.

这篇关于使用 swift 3 在 UIView 上添加阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆