UIImageView 遮罩层不来自角落直到滚动 PageViewController 第一次
UIImageView Mask Layer not coming from corners Until Scroll PageViewController First time
我想达到这个结果
所以我需要在底部添加掩码,但这是我得到的
我试过的代码
private func setupImageCutPath () {
let path = UIBezierPath()
path.move(to: .zero)
path.addLine(to: CGPoint(x: 0 , y: self.imgView.frame.height))
path.addLine(to: CGPoint(x: self.imgView.frame.width , y: self.imgView.frame.height - imageCutAwayPart))
path.addLine(to: CGPoint(x: self.imgView.frame.width, y: 0))
// Multiple tried to check from where masking is comming
self.maskLayer.fillColor = UIColor.gray.cgColor
self.maskLayer.lineWidth = 1.0
self.maskLayer.borderColor = UIColor.gray.cgColor
self.maskLayer.borderWidth = 1.0
self.maskLayer.path = path.cgPath
self.imgView.layer.mask = self.maskLayer
}
滚动后我第一次得到正确的结果
我不明白我缺少什么任何帮助?
我解决了
的问题
path.move(to: .zero)
/* THIS HELPLS ME */ path.addLine(to: CGPoint(x: 0 , y: self.imgView.frame.height - imageCutAwayPart / 2 ))
path.addLine(to: CGPoint(x: self.imgView.frame.width , y: self.imgView.frame.height - imageCutAwayPart))
path.addLine(to: CGPoint(x: self.imgView.frame.width, y: 0))
但是我不明白为什么我需要移动CGPoint(x: 0 , y: self.imgView.frame.height - imageCutAwayPart / 2 )
而不是这个CGPoint(x: 0 , y: self.imgView.frame.height)
如果有人知道这个请post作为答案我一定会接受答案
编辑
Storybaord 中出现了愚蠢但聪明的错误
错误是 UIView 包含屏幕截图中显示的标签。可见的部分是 UIView,我认为它的遮罩无法正常工作。仍然存在第一次在页面视图控制器掩码中不正确但滚动一次后完美的问题。
EDIT2 感谢@DonMag
以下是针对每种情况的有效解决方案
class CutOfImageView:UIImageView {
let imageCutAwayPart:CGFloat = 80
let maskLayer = CAShapeLayer()
override func layoutSubviews() {
super.layoutSubviews()
self.setupImageCutPath()
self.layer.mask = self.maskLayer
}
private func setupImageCutPath () {
let path = UIBezierPath()
path.move(to: .zero)
path.addLine(to: CGPoint(x: 0 , y: self.frame.height ))
path.addLine(to: CGPoint(x: self.frame.width , y: self.frame.height - imageCutAwayPart))
path.addLine(to: CGPoint(x: self.frame.width, y: 0))
path.close()
self.maskLayer.path = path.cgPath
}
}
我想达到这个结果
所以我需要在底部添加掩码,但这是我得到的
我试过的代码
private func setupImageCutPath () {
let path = UIBezierPath()
path.move(to: .zero)
path.addLine(to: CGPoint(x: 0 , y: self.imgView.frame.height))
path.addLine(to: CGPoint(x: self.imgView.frame.width , y: self.imgView.frame.height - imageCutAwayPart))
path.addLine(to: CGPoint(x: self.imgView.frame.width, y: 0))
// Multiple tried to check from where masking is comming
self.maskLayer.fillColor = UIColor.gray.cgColor
self.maskLayer.lineWidth = 1.0
self.maskLayer.borderColor = UIColor.gray.cgColor
self.maskLayer.borderWidth = 1.0
self.maskLayer.path = path.cgPath
self.imgView.layer.mask = self.maskLayer
}
滚动后我第一次得到正确的结果
我不明白我缺少什么任何帮助?
我解决了
的问题 path.move(to: .zero)
/* THIS HELPLS ME */ path.addLine(to: CGPoint(x: 0 , y: self.imgView.frame.height - imageCutAwayPart / 2 ))
path.addLine(to: CGPoint(x: self.imgView.frame.width , y: self.imgView.frame.height - imageCutAwayPart))
path.addLine(to: CGPoint(x: self.imgView.frame.width, y: 0))
但是我不明白为什么我需要移动CGPoint(x: 0 , y: self.imgView.frame.height - imageCutAwayPart / 2 )
而不是这个CGPoint(x: 0 , y: self.imgView.frame.height)
如果有人知道这个请post作为答案我一定会接受答案
编辑
Storybaord 中出现了愚蠢但聪明的错误
错误是 UIView 包含屏幕截图中显示的标签。可见的部分是 UIView,我认为它的遮罩无法正常工作。仍然存在第一次在页面视图控制器掩码中不正确但滚动一次后完美的问题。
EDIT2 感谢@DonMag
以下是针对每种情况的有效解决方案
class CutOfImageView:UIImageView {
let imageCutAwayPart:CGFloat = 80
let maskLayer = CAShapeLayer()
override func layoutSubviews() {
super.layoutSubviews()
self.setupImageCutPath()
self.layer.mask = self.maskLayer
}
private func setupImageCutPath () {
let path = UIBezierPath()
path.move(to: .zero)
path.addLine(to: CGPoint(x: 0 , y: self.frame.height ))
path.addLine(to: CGPoint(x: self.frame.width , y: self.frame.height - imageCutAwayPart))
path.addLine(to: CGPoint(x: self.frame.width, y: 0))
path.close()
self.maskLayer.path = path.cgPath
}
}