2017-04-05 48 views
0

我想「切割孔」在一個UIView一個Swift3的陰影層,iOS的夫特 - 在陰影層切孔

我有一個容器(UIView的),即有2個孩子:

  • 一個的UIImageView
  • 一個對圖像(「疊加」)

我想給覆蓋了一層陰影,並切出陰影的內矩形的頂部的UIView,創建一個發光般的效果在ImageVie的邊緣w^
這是至關重要的輝光是插圖,由於圖像拍攝畫面寬度
我迄今爲止代碼:

let glowView = UIView(frame: CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight)) 
glowView.layer.shadowPath = UIBezierPath(roundedRect: container.bounds, cornerRadius: 4.0).cgPath 
glowView.layer.shouldRasterize = true 
glowView.layer.rasterizationScale = UIScreen.main.scale 
glowView.layer.shadowOffset = CGSize(width: 1.0, height: 1.0) 
glowView.layer.shadowOpacity = 0.4 

container.addSubview(imageView) 
container.addSubview(glowView) 

結果如下所示現在:

image

現在我想剪出較暗的內部部分,這樣只是在邊緣的陰影仍然
任何想法如何實現這一目標?

+0

圖像視圖之前添加輝光視圖。這應該工作 –

+0

對不起,沒有提到它,但我不能這樣做,因爲圖像是所有的寬度和輝光需要嵌入 –

回答

0

感謝Mihai Fratu's answer我能夠創建準確滿足我的需要UIBezierPath
我在這裏發佈我的代碼的情況下,有人有相同的問題後

let innerRadius: CGFloat = 32.0 * UIScreen.main.scale 
let shadowPath: UIBezierPath = UIBezierPath(roundedRect: self.view.bounds, cornerRadius: self.cornerRadius) 
//shadowPath.append(UIBezierPath(roundedRect: self.view.bounds.insetBy(dx: 8, dy: 8), cornerRadius: self.cornerRadius)) 
let shadowWidth: CGFloat = 8.0 // Do this as wide as you want 
var outterPath = UIBezierPath() 
// Start at the top left corner with an x offset of the cornerRadius 
outterPath.move(to: CGPoint(x: self.cornerRadius, y: 0)) 

// Draw a line to the top right corner 
outterPath.addLine(to: CGPoint(x: glowView.bounds.size.width - self.cornerRadius, y: 0)) 
//Draw the round top right corner 
outterPath.addArc(withCenter: CGPoint(x: glowView.bounds.size.width - self.cornerRadius, y: self.cornerRadius), radius: self.cornerRadius, startAngle: (3 * CGFloat.pi)/2, endAngle: 0, clockwise: true) 

// Draw a line to the bottom right corner 
outterPath.addLine(to: CGPoint(x: glowView.bounds.size.width, y: glowView.bounds.size.height - self.cornerRadius)) 
// Draw the round bottom right corner 
outterPath.addArc(withCenter: CGPoint(x: glowView.bounds.size.width - self.cornerRadius, y: glowView.bounds.size.height - self.cornerRadius), radius: self.cornerRadius, startAngle: 0, endAngle: CGFloat.pi/2, clockwise: true) 

// Draw a line to the bottom left corner 
outterPath.addLine(to: CGPoint(x: self.cornerRadius, y: glowView.bounds.size.height)) 
// Draw the round bottom left corner 
outterPath.addArc(withCenter: CGPoint(x: self.cornerRadius, y: glowView.bounds.size.height - self.cornerRadius), radius: self.cornerRadius, startAngle: CGFloat.pi/2, endAngle: CGFloat.pi, clockwise: true) 

// Draw a line to the top left corner 
outterPath.addLine(to: CGPoint(x: 0.0, y: self.cornerRadius)) 
// Draw the round top left corner 
outterPath.addArc(withCenter: CGPoint(x: self.cornerRadius, y: self.cornerRadius), radius: self.cornerRadius, startAngle: CGFloat.pi, endAngle: (3 * CGFloat.pi)/2, clockwise: true) 

// Move to the inner start point and add the paths counterclockwise to prevent the filling of the inner area 
outterPath.move(to: CGPoint(x: shadowWidth + innerRadius, y: shadowWidth)) 
// Draw the inner top left corner 
outterPath.addArc(withCenter: CGPoint(x: shadowWidth + innerRadius, y: shadowWidth + innerRadius), radius: innerRadius, startAngle: (3 * CGFloat.pi)/2, endAngle: CGFloat.pi, clockwise: false) 

// Draw a line to the inner bottom left corner 
outterPath.addLine(to: CGPoint(x: shadowWidth, y: glowView.bounds.size.height - innerRadius - shadowWidth)) 
// Draw the inner bottom left corner 
outterPath.addArc(withCenter: CGPoint(x: shadowWidth + innerRadius, y: glowView.bounds.size.height - innerRadius - shadowWidth), radius: innerRadius, startAngle: CGFloat.pi, endAngle: CGFloat.pi/2, clockwise: false) 

// Draw a line to the inner bottom right corner 
outterPath.addLine(to: CGPoint(x: glowView.bounds.size.width - innerRadius - shadowWidth, y: glowView.bounds.size.height - shadowWidth)) 
// Draw the inner bottom right corner 
outterPath.addArc(withCenter: CGPoint(x: glowView.bounds.size.width - innerRadius - shadowWidth, y: glowView.bounds.size.height - innerRadius - shadowWidth), radius: innerRadius, startAngle: CGFloat.pi/2, endAngle: 0, clockwise: false) 

// Draw a line to the inner top right corner 
outterPath.addLine(to: CGPoint(x: glowView.bounds.size.width - shadowWidth, y: shadowWidth + innerRadius)) 
// Draw the inner top right corner 
outterPath.addArc(withCenter: CGPoint(x: glowView.bounds.size.width - innerRadius - shadowWidth, y: shadowWidth + innerRadius), radius: innerRadius, startAngle: 0, endAngle: (3 * CGFloat.pi)/2, clockwise: false) 

// Draw a line to the inner top left corner 
outterPath.addLine(to: CGPoint(x: shadowWidth + innerRadius, y: shadowWidth)) 
outterPath.close() 
1

嘗試使用此作爲爲你的身影路徑:

let shadowWidth = 2.0 // Do this as wide as you want 
var outterPath = UIBezierPath() 
outterPath.move(to: CGPoint(x: shadowWidth, y: 0)) 
outterPath.addLine(to: CGPoint(x: glowView.bounds.size.width, y: 0)) 
outterPath.addLine(to: CGPoint(x: glowView.bounds.size.width, y: glowView.bounds.size.height)) 
outterPath.addLine(to: CGPoint(x: 0.0, y: glowView.bounds.size.height)) 
outterPath.addLine(to: CGPoint(x: 0.0, y: 0.0)) 
outterPath.addLine(to: CGPoint(x: shadowWidth, y: 0.0)) 
outterPath.addLine(to: CGPoint(x: shadowWidth, y: glowView.bounds.size.height - shadowWidth)) 
outterPath.addLine(to: CGPoint(x: glowView.bounds.size.width - shadowWidth, y: glowView.bounds.size.height - shadowWidth)) 
outterPath.addLine(to: CGPoint(x: glowView.bounds.size.width - shadowWidth, y: shadowWidth)) 
outterPath.addLine(to: CGPoint(x: shadowWidth, y: shadowWidth)) 
outterPath.close() 

這不會創建一個圓角的矩形但變化的代碼一點點上面,你應該能夠添加這些呢。

+0

沒有什麼改變不幸的 –

+0

我已經更新了我的答案 –

+1

謝謝,那是我的方法非常好的基礎!我會做出回答,並在那裏引用你的答案! –