如何使用Shadow View iOS Swift快速实现这种转角重做?

时间:2019-07-15 08:14:47

标签: swift uiview

我正在尝试设计一个带有阴影的左上角半径和右下角半径的设计。我能够使阴影也围绕视图的拐角半径。但是当我尝试仅给侧面阴影赋予拐角半径或半径未显示时。野兔我做了什么

 extension UIview {
   //corner radius function
       func roundCornersForView(corners:UIRectCorner, radius: CGFloat) {

        DispatchQueue.main.async {
            let path = UIBezierPath(roundedRect: self.bounds,
                                    byRoundingCorners: corners,
                                    cornerRadii: CGSize(width: radius, height: radius))
            let maskLayer = CAShapeLayer()
            maskLayer.frame = self.bounds
            maskLayer.path = path.cgPath
            self.layer.mask = maskLayer
        }
    }


    func setShadow(){
        self.layer.masksToBounds = false
        // set the shadow properties
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowOffset = CGSize(width: 0, height: 1.0)
        self.layer.shadowOpacity = 0.2
        self.layer.shadowRadius = 4.0
        self.roundCornersForView(corners: [.topLeft, .bottomRight], radius: 20)

    }



}

 /// finally call it table cell


  cell?.BackView.setShadow()

here is the design what i want to do

1 个答案:

答案 0 :(得分:0)

1)为什么要编写一个单独的函数来绘制rect,然后在您的视图中应用cornerRadius蒙版?将函数的内容替换为:

yourView.layer.masksToBounds = true
yourView.layer.cornerRadius = 20
yourView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMaxYCorner]

2)一旦遮盖了视图的角并将其masksToBounds属性设置为true,就可以将阴影设置到视图的图层,并且它将根据需要自动环绕圆角:-< / p>

yourView.layer.shadowOffset = CGSize(width: 0, height: -0.5)
yourView.layer.shadowRadius = 1
yourView.layer.shadowColor = UIColor(red:0.01, green:0.05, blue:0.09, alpha:0.18).cgColor
yourView.layer.shadowOpacity = 0.8

3)建议在单元类的awakeFromNib()init()方法中使用单元的UI属性(在您的情况下为子视图的cornerRadius和shadow),而不要在委托方法中使用它们调用了很多次,因此破坏了整个可重用性

已经测试了应用阴影的代码,对我来说很好用。希望这会有所帮助!