2016-12-30 65 views
1

我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的形狀後,可見???

回答

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; 
+0

謝謝你的迴應,我試圖通過設置邊界的陰影,但陰影出現在我的imageView裏面,任何想法我怎麼能讓它在imageView之外? –

+0

在蒙版上應用陰影 - mask.shadowColor = UIColor.red.cgColor mask.shadowRadius = 5.0; mask.shadowOpacity = 2; mask.shadowOffset = CGSize(寬度:2,高度:3) – Darshana