由于仅变换图层

时间:2018-01-04 20:11:03

标签: ios swift uiview swift4

我想在MyCustomView周围绘制以这种方式创建的阴影:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = MyCustomView()
    header.contentView.dropShadow() // tried also header.dropShadow()
    return header
}

以下是定义:

class MyCustomView: UIView {
    //...
    var contentView = UIView()

    init() {
        super.init(frame: CGRect(
            x: CGFloat(0), y: CGFloat(0),
            width: CGFloat(200), height: CGFloat(200)))

        //...        
        let marginGuide = self.layoutMarginsGuide

        contentView.addSubview(someLabel)
        contentView.addSubview(anotherLabel)

        // Background colors
        self.backgroundColor = UIColor.init(hex: "EBEBF1")
        contentView.backgroundColor = .white

        // translatesAutoresizingMaskIntoConstraints
        contentView.translatesAutoresizingMaskIntoConstraints = false

        // contentView constraints
        contentView.topAnchor.constraint(
            equalTo: marginGuide.topAnchor, constant: CGFloat(0)).isActive = true
        contentView.bottomAnchor.constraint(
            equalTo: marginGuide.bottomAnchor, constant: marginSize).isActive = true
        contentView.trailingAnchor.constraint(
            equalTo: marginGuide.trailingAnchor, constant: -marginSize).isActive = true
        contentView.leadingAnchor.constraint(
            equalTo: marginGuide.leadingAnchor, constant: CGFloat(7)).isActive = true
        }
    }

extension UIView {
    func dropShadow(scale: Bool = true) {
        self.layer.masksToBounds = false
        self.layer.shadowColor = UIColor.darkGray.cgColor
        self.layer.shadowOpacity = 0.5
        self.layer.shadowOffset = CGSize(width: 0, height: -1)
        self.layer.shadowRadius = 3

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

而不是影子我收到警告:

<CATransformLayer: 0x60400023e780> - changing property shadowColor in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shadowOpacity in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shadowOffset in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shadowPath in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shouldRasterize in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property rasterizationScale in transform-only layer, will have no effect <CATransformLayer: 0x60400023e780> - changing property shadowPath in transform-only layer, will have no effect

你知道为什么会这样吗?

1 个答案:

答案 0 :(得分:2)

尝试在MyCustomView的layoutSubviews方法中调用dropShadow()函数:

class MyCustomView: UIView {

//.. your implementation 

override func layoutSubviews() {
    super.layoutSubviews()
    contentView.dropShadow()
}