仅在底部UIView上设置阴影

时间:2017-03-30 03:07:05

标签: ios uiview shadow

我想在UIView上创建仅底部阴影。现在使用此功能,将在顶部,底部,左侧和右侧创建阴影。

help(pl.tools.make_subplots)

无论如何只在底部创建阴影? 任何帮助,将不胜感激。谢谢!

9 个答案:

答案 0 :(得分:30)

我认为阴影的正确思考方式是,阴影属于对象,即按钮,uiview,而不仅仅是侧面的一部分。想象有一个虚拟光源。你不能真正为一方创造一个阴影。

话虽如此,影子将始终是整个视图的阴影。但是,您可以更改阴影偏移以使其朝向底部。

view.layer.shadowOffset = CGSize(width: 0, height: 3)

这意味着您希望光源从顶部拍摄光线以使阴影到达底部。你仍然在顶部看到一些阴影的原因是阴影半径。这是为了模拟光的diffuse。光线散射越多,阴影就越柔和,所以你仍会看到顶影。

view.layer.shadowRadius = 1 or 0.5

尝试减少半径。它会给你一个更好的视觉效果。

如果需要,要了解本影,半影和antumbra,请查看https://en.wikipedia.org/wiki/Umbra,_penumbra_and_antumbra

答案 1 :(得分:9)

更改shadowOffset

 view.layer.shadowOffset = CGSize(width: 0, height: 3)

答案 2 :(得分:6)

这是在Swift中应用阴影的正确方法:

yourView.layer.shadowOffset = CGSize(width: 0, height: 3)
yourView.layer.shadowOpacity = 0.6
yourView.layer.shadowRadius = 3.0
yourView.layer.shadowColor = UIColor.red.cgColor

答案 3 :(得分:5)

此代码适用于swift 4,阴影适用于视图底部:

view.layer.masksToBounds = false
view.layer.shadowRadius = 4
view.layer.shadowOpacity = 1
view.layer.shadowColor = UIColor.gray.cgColor
view.layer.shadowOffset = CGSize(width: 0 , height:2)

答案 4 :(得分:3)

这只会在底部添加阴影。实现为UIView的扩展

extension UIView {
func addBottomShadow() {
    layer.masksToBounds = false
    layer.shadowRadius = 4
    layer.shadowOpacity = 1
    layer.shadowColor = UIColor.gray.cgColor
    layer.shadowOffset = CGSize(width: 0 , height: 2)
    layer.shadowPath = UIBezierPath(rect: CGRect(x: 0,
                                                 y: bounds.maxY - layer.shadowRadius,
                                                 width: bounds.width,
                                                 height: layer.shadowRadius)).cgPath
}
}

enter image description here

答案 5 :(得分:1)

旧问题,但似乎没有一个答案能真正回答这个问题。 尽管上述答案在shadowRadius较低的情况下仍然有效,但您并没有真正获得“阴影”效果。

您可以使用UIBezierPath

绝对在底部添加阴影

在这里-

    let shadowWidth: CGFloat = 1.2 // Shadow width, will be the width furthest away from the view, this is equivalent to 120% of the views width
    let shadowHeight: CGFloat = 0.3 // Shadow height, again this is equivalent to 30%
    let shadowRadius: CGFloat = 5 
    let width = someView.frame.width
    let height = someView.frame.height // Get width and height of the view

    // Plot the path
    let shadowPath = UIBezierPath()
    shadowPath.move(to: CGPoint(x: shadowRadius / 2, y: height - shadowRadius / 2))
    shadowPath.addLine(to: CGPoint(x: width - shadowRadius / 2, y: height - shadowRadius / 2))
    shadowPath.addLine(to: CGPoint(x: width * shadowWidth, y: height + (height * shadowHeight)))
    shadowPath.addLine(to: CGPoint(x: width * -(shadowWidth - 1), y: height + (height * shadowHeight)))
    // Add shadow
    someView.layer.shadowPath = shadowPath.cgPath
    someView.layer.shadowRadius = shadowRadius
    someView.layer.shadowOffset = .zero
    someView.layer.shadowOpacity = 0.2

这将输出

enter image description here

或者,如果您想要一个更简单的解决方案,则可以选择较少的选项

   let buttonHeight = someButton.frame.height
    let buttonWidth = someButton.frame.width

    let shadowSize: CGFloat = 15
    let contactRect = CGRect(x: -shadowSize, y: buttonHeight - (shadowSize * 0.2), width: buttonWidth + shadowSize * 2, height: shadowSize)
    someButton.layer.shadowPath = UIBezierPath(ovalIn: contactRect).cgPath
    someButton.layer.shadowRadius = 5
    someButton.layer.shadowOpacity = 0.6

哪个会输出

enter image description here

此处示例

https://github.com/hemo87/ExampleShadow/tree/master

答案 6 :(得分:0)

如果您确实只希望在UIView的一侧放置阴影,则应将view.layer.shadowPath设置为UIBezierPath

这里是一个仅在视图底部显示阴影的示例:

view.layer.shadowPath = UIBezierPath(rect: CGRect(x: 0,
                                                  y: bounds.maxY - layer.shadowRadius,
                                                  width: bounds.width,
                                                  height: layer.shadowRadius)).cgPath

解构CGRect值,您得到:

  • xwidth确保阴影占据了视图的整个水平宽度(您可能希望对其进行调整,例如使用layer.shadowRadius值作为偏移的基础)
  • yheight确保阴影从尽可能低的位置开始,然后仅与半径一样大

当然,在某些情况下,这是行不通的,例如,当您希望shadowRadius大于视图的height时。在这种情况下,我建议使用图像视图或遮罩层。

希望这会有所帮助,

答案 7 :(得分:0)

class ViewBottomShadow: UIView {
  init() {
    super.init(frame: .zero)
    backgroundColor = .white
    layer.masksToBounds = false
    layer.shadowRadius = 2
    layer.shadowOpacity = 1
    layer.shadowColor = UIColor.gray.cgColor
    layer.shadowOffset = CGSize(width: 0 , height:2)
  }

  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

enter image description here

答案 8 :(得分:0)

只需绘制规则阴影并将其上下颠倒即可,

@objc func shadowView() -> UIView {
        let shadowView = UIView(frame: .zero)
        shadowView.backgroundColor = .white
        shadowView.layer.shadowColor = UIColor.grey.cgColor
        shadowView.layer.shadowOffset = CGSize(width: 0, height: 2)
        shadowView.layer.shadowOpacity = 1.0
        shadowView.layer.shadowRadius = 4
        shadowView.layer.compositingFilter = "multiplyBlendMode"
        return shadowView
    }

func idtm_addBottomShadow() {
        let shadow = shadowView()
        shadow.transform = transform.rotated(by: 180 * CGFloat(Double.pi))
        shadow.transform = transform.rotated(by: -1 * CGFloat(Double.pi))
        shadow.translatesAutoresizingMaskIntoConstraints = false
        addSubview(shadow)
        NSLayoutConstraint.activate([
            shadow.leadingAnchor.constraint(equalTo: leadingAnchor),
            shadow.trailingAnchor.constraint(equalTo: trailingAnchor),
            shadow.bottomAnchor.constraint(equalTo: bottomAnchor),
            shadow.heightAnchor.constraint(equalToConstant: 1),
            ])
    }