2017-07-17 55 views
0

我正在尝试将角半径添加到BottmLeft和bottomRight角,并且还为其添加阴影。但由于某些原因,如果我添加角落半径,阴影消失。这是什么原因?设置角落半径时未应用阴影

这是我做的:

我有扩展:

extension UIView { 
    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) { 
     self.layoutIfNeeded() 
     let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) 
     let mask = CAShapeLayer() 
     mask.path = path.cgPath 
     self.layer.mask = mask 
    } 
} 

应用的角落:

myView.roundCorners([.bottomLeft, .bottomRight], radius: 35

添加阴影延伸:

func addShadow(offset: CGSize, color: UIColor, radius: CGFloat, opacity: Float) { 
    let layer = self.layer 
    layer.masksToBounds = false 
    layer.shadowOffset = offset 
    layer.shadowColor = color.cgColor 
    layer.shadowRadius = radius 
    layer.shadowOpacity = opacity 
    layer.shadowPath = UIBezierPath.init(roundedRect: layer.bounds, cornerRadius: layer.cornerRadius).cgPath 

    let backgroundCGColor = self.backgroundColor?.cgColor 
    self.backgroundColor = nil 
    layer.backgroundColor = backgroundCGColor 
} 

并添加阴影:

myView.addShadow(offset: CGSize.init(width: 0.0, height: 10.0) , color: UIColor.blue, radius: 35.0, opacity: 1.0) 

为什么添加圆角后阴影消失?

+0

尝试添加self.clipsToBounds = false – ignotusverum

回答

0

myView.layer。 shadowPath需要使用UIBezierPath进行初始化。只要给它与myView相同的框架和边界。

+0

我已经这样做了。 –

相关问题