【问题标题】:shadow not visible on view with custom shape自定义形状的视图中不可见阴影
【发布时间】:2016-12-30 11:42:15
【问题描述】:

在我将它的形状更改为六边形后,我的 imageview 上的阴影不可见,这就是我如何更改 imageView 的形状:

extension UIView {


func makeHexagon(){

    let lineWidth: CGFloat = 3
    let path = UIBezierPath(roundedPolygonPathWithRect: self.bounds, lineWidth: lineWidth, sides: 6, cornerRadius: 1)

    let mask = CAShapeLayer()


    mask.path = path.cgPath
    mask.lineWidth = lineWidth
    mask.strokeColor = UIColor.clear.cgColor //controls the  stroke width
    mask.fillColor = UIColor.white.cgColor
    self.layer.mask = mask




    let border = CAShapeLayer()
    border.path = path.cgPath
    border.lineWidth = lineWidth
    border.strokeColor = UIColor.black.cgColor
    border.fillColor = UIColor.clear.cgColor


    self.layer.addSublayer(border)

}

}

.......................

 extension UIBezierPath {

    convenience init(roundedPolygonPathWithRect rect: CGRect, lineWidth: CGFloat, sides: NSInteger, cornerRadius: CGFloat) {

        self.init()



        let theta = CGFloat(2.0 * M_PI) / CGFloat(sides)
        let offSet = CGFloat(cornerRadius) / CGFloat(tan(theta/2.0))
        let squareWidth = min(rect.size.width, rect.size.height)

        var length = squareWidth - lineWidth

        if sides%4 != 0 {
            length = length * CGFloat(cos(theta / 2.0)) + offSet/2.0
        }
        let sideLength = length * CGFloat(tan(theta / 2.0))

        var point = CGPoint(x: squareWidth / 2.0 + sideLength / 2.0 - offSet, y: squareWidth - (squareWidth - length) / 2.0)
        var angle = CGFloat(M_PI)
        move(to: point)

        for _ in 0 ..< sides {
            point = CGPoint(x: point.x + CGFloat(sideLength - offSet * 2.0) * CGFloat(cos(angle)), y: point.y + CGFloat(sideLength - offSet * 2.0) * CGFloat(sin(angle)))
            addLine(to: point)

            let center = CGPoint(x: point.x + cornerRadius * CGFloat(cos(angle + CGFloat(M_PI_2))), y: point.y + cornerRadius * CGFloat(sin(angle + CGFloat(M_PI_2))))
            addArc(withCenter: center, radius:CGFloat(cornerRadius), startAngle:angle - CGFloat(M_PI_2), endAngle:angle + theta - CGFloat(M_PI_2), clockwise:true)

            point = currentPoint // we don't have to calculate where the arc ended ... UIBezierPath did that for us
            angle += theta
        }

        close()
    }
}

使用扩展:

        firstImageView.makeHexagon()

这就是我在 ImageView 上添加阴影效果的方式:

     firstImageView.layer.contentsScale = UIScreen.main.scale;
    firstImageView.layer.shadowColor = UIColor.black.cgColor;
    firstImageView.layer.shadowOffset = CGSize.zero;
    firstImageView.layer.shadowRadius = 5.0;
    firstImageView.layer.shadowOpacity = 2;
    firstImageView.layer.masksToBounds = false;
    firstImageView.clipsToBounds = false;

任何人都可以指出我的阴影在改变 imageView 的形状后不可见???

【问题讨论】:

    标签: ios swift3 uibezierpath dropshadow


    【解决方案1】:

    您已将视图裁剪为六边形,因此要显示阴影,您必须在 ShapeLayer 上设置阴影。

    let border = CAShapeLayer()
    border.path = path.cgPath
    border.lineWidth = lineWidth
    border.strokeColor = UIColor.black.cgColor
    border.fillColor = UIColor.clear.cgColor
    border.shadowColor = UIColor.red.cgColor;
    border.shadowRadius = 5.0;
    border.shadowOpacity = 2;
    

    【讨论】:

    • 感谢您的回复,我尝试为边框设置阴影,但阴影出现在我的 imageView 内部,知道如何让它在 imageView 之外?
    • 在蒙版上应用阴影 -- mask.shadowColor = UIColor.red.cgColor mask.shadowRadius = 5.0; mask.shadowOpacity = 2; mask.shadowOffset = CGSize(width: 2, height: 3)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多