UIImageView蒙版图层直到滚动PageViewController第一次之前都不是来自角落

时间:2018-12-14 11:45:25

标签: ios swift uiimageview layer

我想达到这个结果

enter image description here

所以我需要在底部添加蒙版,但在这里我得到了

enter image description here

我尝试过的代码

 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

    }

滚动后,第一次我得到正确的结果

我不明白我缺少任何帮助吗?

1 个答案:

答案 0 :(得分:0)

我用

解决了问题
    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)

如果有人知道这一点,请张贴为答案,我一定会接受答案

enter image description here

编辑

Storybaord中有一个愚蠢但明智的错误

Mistake的UIView包含屏幕快照中显示的Label。可见的部分是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



    }
}