swift - 向CALayer添加多个 mask (1用于添加角点)

标签 swift calayer uibezierpath

我想做这样的用户界面

Custom View

我希望这个 View 有圆角和阴影。

这是我在 Xib 中的 View 层次结构:

-Content View 
    - Shadow View
    - Container View
        - Left View
        - Concave View
        - Action View

这是我实现当前 UI 的代码片段:

对于凹路径:

        let couponPath = UIBezierPath()
        let zeroPoint = CGPoint.zero
        couponPath.move(to: zeroPoint)
        couponPath.addLine(to: CGPoint(x: leftView.frame.width, y: zeroPoint.y))
        let radius = concaveView.frame.width / 2
        let centerX = zeroPoint.x + leftView.frame.width + radius
        couponPath.addArc(withCenter: CGPoint(x: centerX, y: zeroPoint.y), radius: radius, startAngle: CGFloat.pi, endAngle: 0, clockwise: false)
        couponPath.addLine(to: CGPoint(x: containerView.frame.width, y: zeroPoint.y))

        couponPath.addLine(to: CGPoint(x: containerView.frame.width, y: containerView.frame.height))

        couponPath.addArc(withCenter: CGPoint(x: leftView.frame.width + radius, y: containerView.frame.height), radius: radius, startAngle: CGFloat.pi * 0, endAngle: CGFloat.pi, clockwise: false)

        couponPath.addLine(to: CGPoint(x: zeroPoint.x, y: containerView.frame.height))
        couponPath.close()

然后我创建CALayer,路径为couponPath

let shapeLayer = CAShapeLayer()
shapeLayer.path = couponPath.cgPath

我将它屏蔽到我的containerView: self.containerView.layer.mask = shapeLayer

我尝试使用此代码添加阴影。

let shadowLayer = CAShapeLayer()
shadowLayer.frame = containerView.frame
shadowLayer.path = shapeLayer.path
shadowLayer.shadowOpacity = 0.5
shadowLayer.shadowRadius = 5
shadowLayer.masksToBounds = false
shadowLayer.shadowOffset = CGSize(width: 0, height: 20)
self.shadowView.layer.addSublayer(shadowLayer)

如何添加另一个 mask 来将容器 View 的半径转角? 有没有更好的方法来添加阴影而不是使用新 View (ShadowView)并在其上应用阴影?

这是我的 xib 和 View Gist

谢谢。

最佳答案

这应该可以帮助您处理阴影和圆角。您设置 ShadowView 图层而不是添加子图层。

class RoundedCornerView: UIView {

    var shadowView: UIView?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    func setup() {

        // Set the layer of the main view to have rounded corners
        layer.cornerRadius = 12.0
        layer.masksToBounds = true

        // Create a shadow view and its mask
        shadowView = UIView(frame: bounds)
        addSubview(shadowView!)
        sendSubviewToBack(shadowView!)

        shadowView?.layer.shadowColor = UIColor.black.cgColor
        shadowView?.layer.shadowOffset = 4.0
        shadowView?.layer.shadowOpacity = 0.4
        shadowView?.layer.shadowRadius = 12.0
        shadowView?.layer.shadowPath = UIBezierPath(roundedRect: layer.bounds, cornerRadius: layer.cornerRadius).cgPath

        shadowView?.layer.masksToBounds = false
        shadowView?.clipsToBounds = false

    }

}

关于swift - 向CALayer添加多个 mask (1用于添加角点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52417714/

相关文章:

ios - 从 TableView 为索引部分和行传递正确的 Realm 对象时出现问题

swift - 无法将类型 Object<String> 的返回表达式转换为返回类型 Object<Character>

ios - SWRevealViewController 与 TabbarController 设置为 rootViewController

ios Swift Google map sdk 在 iPhone 6s 中无法工作

ios - 从远程位置加载 CGpath/UIBezierPath

iphone - 连续显示多个 CALayer 60 秒?

cocoa——让嵌套的 NSView 和 CALayers 按比例调整大小

ios - CoreAnimation - 不透明度淡入淡出动画不起作用

ios - 未能对 UIBezierPath 进行描边()

ios - 如何在 CAShapeLayer 上快速循环显示颜色?